Javascript对象数组检查密钥

时间:2017-04-09 20:58:31

标签: javascript arrays typescript

enter image description here

所以我的问题是如何通过" key"来检查这个对象数组?例如,我想检查密钥3892是否存在,我已尝试使用indexOf,但没有运气,我无法使用循环。

2 个答案:

答案 0 :(得分:2)

您可以Object.keysArray.prototype.includes链接以实现

Object.keys(myObject).includes(myKey);



const myObject = { name: 'Peter' };
const myKey = 'name';

const result = Object.keys(myObject).includes(myKey);

console.log(`Includes key ${myKey}? `,  result);




答案 1 :(得分:2)

您可以使用some()hasOwnProperty()



var array = [{3892: 'value'}, {1234: 'value'}];

var check = array.some(obj => obj.hasOwnProperty(3892));
console.log(check)