如何在 JavaScript 对象或数组中检查特定键是否存在?


在 JavaScript 中,对象以键值对的形式存在。对象的键被称为该对象的属性,并用字符串表示。对象的属性可以具有任何数据类型的值。

例如,如果创建一个员工对象,则它具有员工姓名、员工 ID、员工年龄、工资等属性。这些是员工对象的属性,称为键。这些属性的值对于不同的员工将是不同的。

对于数组,键是索引,如果存在给定的索引,则该索引处将有一个值。

可以通过使用一些函数和运算符来检查 JavaScript 中对象或数组中键的存在。

使用 in 运算符

in 运算符仅检查对象的给定键,并返回布尔结果,即如果对象具有给定键则返回“true”,否则返回“false”。

语法

in 运算符的语法如下所示。

For objects:
key in objectName //returns true if the object has the (key)objectProperty

示例

此示例演示了如何使用 in 运算符检查对象中是否存在键。

let employee = { firstName: 'Mohammed', lastName: 'Abdul Rawoof', id : 1000, designation: 'Intern Software Engineer', salary : 18000 }; console.log("The given employee details are: ",employee) console.log("Is firstName present in employee:",'firstName' in employee); console.log("Is employee age present in employee:",'age' in employee) console.log("Is 18000 present in employee:", 18000 in employee)

数组的 in 运算符

在数组中,如果在给定索引处存在值,则 in 运算符将返回 true,否则返回 false。

语法

For arrays:
Index in arrayName //returns true if the given index has a value

示例

此示例演示了如何使用 in 运算符检查数组中是否存在键。

function getKeyArr(a,indx){ console.log("The given array with its length is:",a,a.length) if(indx in a){ console.log("The array has a key at the given index",indx) } else{ console.log("The array doesn't have a key at the given index",indx," as its array length is:",a.length) } } console.log("Checking the existance of a key in an array using hasOwnProperty()") getKeyArr([12,56,33,2,7],4) getKeyArr([12,56,33,2,7],8) getKeyArr([1,2,4,53,36,7,83,90,45,28,19],16)

使用 hasOwnProperty() 函数

hasOwnProperty() 函数将检查给定对象中键的存在,如果键存在则返回 true,否则返回 false。此函数将对象的键作为参数,并相应地返回布尔结果。

语法

这是 hasOwnProperty() 函数的语法。

For object:
objectName.hasOwnPropert(key) //key is object property.

示例 3

此示例演示了如何使用 hasOwnProperty() 检查对象中的键。

let employee = { emp_name: 'Abdul Rawoof', emp_id: 1000, role: 'Software Engineer', salary: 18000 }; console.log("The given employee details are:", employee) console.log("Checking the keys present in an object using hasOwnProperty:") function getEmpDet(str){ if(employee.hasOwnProperty(str)){ console.log("The given employee object has",str) } else{ console.log("The given employee object doesn't have",str) } } getEmpDet('emp_id') getEmpDet('salary') getEmpDet('designation')

数组的 hasOwnProperty()

在数组中,如果在给定索引处存在值,则 in 运算符将返回 true,否则返回 false。而 hasOwnProperty() 方法可以检查数组中是否存在索引,但不检查是否为空。

语法

以下是数组的 hasOwnProperty() 的语法。

arrayName.hasOwnProperty(index)

示例

此示例演示了如何使用 hasOwnProperty 检查数组中是否存在键。

function getKeyArr(a,indx){ console.log("The given array with its length is:",a,a.length) if(a.hasOwnProperty(indx)){ console.log("The array has a key at the given index",indx) } else{ console.log("The array doesn't have a key at the given index",indx," as its array length is:",a.length) } } console.log("Checking the existance of a key in an array using hasOwnProperty()") getKeyArr([12,56,33,2,7],4) getKeyArr([12,56,33,2,7],8) getKeyArr([1,2,4,53,36,7,83,90,45,28,19],16)

更新于:2022年8月26日

11K+ 次浏览

启动您的 职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.