检查 JavaScript 对象中是否存在某个键?
在开发中,我们经常需要检查某个键是否存在于对象中。你可能知道一两种方法,但还有一些其他方法值得探索。如果你想了解更多,请继续阅读。
在这篇博客中,我将演示执行此检查的四种不同方法。每种方法都有其优缺点,因此了解所有方法至关重要。这样,当需要时,我们就可以选择最合适的方法。
使用in
运算符。
JavaScript 中的in
操作用于检查对象中是否存在某个属性。true
如果存在,则返回 if ,否则返回 else false
。
注意:它还包含来自继承或原型链的属性。因此,当你需要纯粹检查对象本身的属性时,它可能无法返回所需的结果。
复杂性是O(1)
。
const user = {
fullName: "Shakil Ahmed",
age: 22,
isStudent: true,
};
// structure "keyName" in object
console.log("fullName" in user); // true
console.log("age" in user); // true
console.log("institute" in user); // false
使用hasOwnProperty
方法。
hasOwnProperty
是一个对象方法,其用途与in
运算符相同。但不同之处在于,它仅检查对象自身的属性,而不检查继承或原型链属性。因此它返回纯结果。
将键名放在方法参数中,true
如果包含键则返回,否则返回false
。
复杂度较高O(1)
,所以性能也不错。
const user = {
fullName: "Shakil Ahmed",
age: 22,
isStudent: true,
};
console.log(user.hasOwnProperty("fullName")); // true
console.log(user.hasOwnProperty("age"));// true
console.log(user.hasOwnProperty("institute")); // false
使用可选链式调用
可选链式调用是 ES11 的一项功能,它有助于访问值而不会抛出Uncaught TypeError
错误。当键不存在时,返回undefined
;否则,返回值。要将结果转换为布尔值,请将其添加!!
到表达式的前面。
检查的复杂度O(1)
为,因此性能良好。
const user = {
fullName: "Shakil Ahmed",
age: 22,
isStudent: true,
};
console.log(user?.fullName); // Shakil Ahmed
console.log(!!user?.fullName); // true
console.log(user?.id); // undefined
console.log(!!user?.id); // false
但是,它有一个限制:如果值为false
、、或任何假值0
,它将返回,即使对象中存在该键。undefined
null
false
const user = {
fullName: "Shakil Ahmed",
age: 22,
isStudent: false,
a: 0,
b: undefined,
c: null,
d: NaN,
};
console.log(user?.isStudent); // false
console.log(!!user?.isStudent); // false
console.log(user?.a); // 0
console.log(!!user?.a); // false
console.log(user?.b); // undefined
console.log(!!user?.b); // false
console.log(user?.c); // null
console.log(!!user?.c); // false
console.log(user?.d); // NaN
console.log(!!user?.d); // false
在上面的例子中,对象中存在isStudent
、a
、b
、变量c
,但这种技术仅仅由于假值而返回。d
false
比较所有键
此过程涉及手动遍历对象的所有键以检查特定键是否存在。
复杂度:遍历整个对象的时间复杂度为O(n)
,其中n
是对象中的键的数量。
const user = {
fullName: "Shakil Ahmed",
age: 22,
isStudent: false,
a: 0,
b: undefined,
c: null,
d: NaN,
};
const isKeyExistInObject = (obj, key) => {
// here '!!' is to convert result in boolean you also can use Boolean() constructor.
return !!Object.keys(obj).find((eachKey) => eachKey === key);
};
在上面的代码中,我创建了一个函数来检查某个键是否存在。在这里,我使用 将对象的所有键转换为一个数组Object.keys(obj)
。然后,我遍历该数组并检查它是否包含指定的键。如果找到该键,find
则返回eachKey
;否则,返回undefined
。使用!!
将此结果转换为布尔值。您也可以使用Boolean(value)
构造函数。
该isKeyExistInObject
函数返回一个布尔值。
console.log(isKeyExistInObject(user, "fullName")); // true
console.log(isKeyExistInObject(user, "first name")); // false
console.log(isKeyExistInObject(user, "age")); // true
console.log(isKeyExistInObject(user, "isStudent")); // true
console.log(isKeyExistInObject(user, "a")); // true
console.log(isKeyExistInObject(user, "b")); // true
console.log(isKeyExistInObject(user, "c")); // true
console.log(isKeyExistInObject(user, "d")); // true
console.log(isKeyExistInObject(user, "notExist")); // false
注意:虽然此方法比其他技术稍微复杂一些,但它适用于任何虚假值。
嵌套对象的示例
这是一个嵌套对象,我们将在这里使用上述所有技术
示例对象:
const user = {
name: "Shakil",
age: 22,
studentData: {
id: "123",
bachelor: "CSE",
countryData: {
country: "Bangladesh",
capital: "Dhaka",
language: "Bangla",
},
},
};
使用in
:
// ========== is "name" exist
console.log("name" in user); // true
// ========== is "studentData" exist
console.log("studentData" in user); // true
// ========== is "id" exist
console.log(
"studentData" in user && "id" in user.studentData
); // true
// ========== is "language" exist
console.log(
"studentData" in user &&
"countryData" in user.studentData &&
"language" in user.studentData.countryData
); // true
使用hasOwnProperty
:
// ========== is "name" exist
console.log(user.hasOwnProperty("name")); // true
// ========== is "studentData" exist
console.log(user.hasOwnProperty("studentData")); // true
// ========== is "id" exist
console.log(
user.hasOwnProperty("studentData") &&
user.studentData.hasOwnProperty("id")
); // true
// ========== is "language" exist
console.log(
user.hasOwnProperty("studentData") &&
user.studentData.hasOwnProperty("countryData") &&
user.studentData.countryData.hasOwnProperty("language")
); // true
使用可选链式调用
// ========== is "name" exist
console.log(!!user?.name); // true
// ========== is "studentData" exist
console.log(!!user?.studentData); // true
// ========== is "id" exist
console.log(
!!user?.studentData && !!user.studentData?.id
); // true
// ========== is "language" exist
console.log(
!!user?.studentData &&
!!user.studentData?.countryData &&
!!user.studentData.countryData?.language
); // true
比较所有键:
const isKeyExistInObject = (obj, key) => {
return !!Object.keys(obj).find(
(eachKey) => eachKey === key
);
};
// ========== is "name" exist
console.log(isKeyExistInObject(user, "name")); // true
// ========== is "studentData" exist
console.log(isKeyExistInObject(user, "studentData")); // true
// ========== is "id" exist
console.log(
isKeyExistInObject(user, "studentData") &&
isKeyExistInObject(user.studentData, "id")
); // true
// ========== is "language" exist
console.log(
isKeyExistInObject(user, "studentData") &&
isKeyExistInObject(user.studentData, "countryData") &&
isKeyExistInObject(user.studentData.countryData, "language")
); // true
总而言之,了解检查 JavaScript 对象中键的不同方法,可以帮助开发者编写更高效、更可靠的代码。每种方法都有其自身的优势,能够灵活地处理各种场景。熟悉这些技巧,可以提升你的 JavaScript 技能,轻松应对与对象相关的挑战。继续探索和尝试,找到最适合你需求的方法。祝你编程愉快!
鏂囩珷鏉ユ簮锛�https://dev.to/developerhub/level-up-your-javascript-mastering-object-property-checks-1d4h