admin管理员组文章数量:1297182
I need to determine if a certain key exists in an array of objects.
Here is a sample array:
arrOfObj = [{
"mainKey1": {
"subKey1": {
"innerKey1": {
"innerMostKey1": {
"key1": "value"
}
}
}
}
}, {
"mainKey2": {
"key2": "value"
}
}, {
"mainKey3": {
"subKey3": {
"key3": "value"
}
}
}
]
I was trying to do this but I get the wrong output:
const objKeys = Object.keys(arrOfObj)
console.log('objKeys = ' + JSON.stringify(arrOfObj))
Output is the index numbers:
objKeys = ["0", "1", "2"]
I want to have a function that works like this:
var isKeyPresent = checkKeyPresenceInArray('mainKey3')
Please note though that I only need to check the topmost level in the objects - in above example, these are the main keys (mainKey1
, etc) and that their content is dynamic (some others have deeply nested object inside and some not so.
Help!
I need to determine if a certain key exists in an array of objects.
Here is a sample array:
arrOfObj = [{
"mainKey1": {
"subKey1": {
"innerKey1": {
"innerMostKey1": {
"key1": "value"
}
}
}
}
}, {
"mainKey2": {
"key2": "value"
}
}, {
"mainKey3": {
"subKey3": {
"key3": "value"
}
}
}
]
I was trying to do this but I get the wrong output:
const objKeys = Object.keys(arrOfObj)
console.log('objKeys = ' + JSON.stringify(arrOfObj))
Output is the index numbers:
objKeys = ["0", "1", "2"]
I want to have a function that works like this:
var isKeyPresent = checkKeyPresenceInArray('mainKey3')
Please note though that I only need to check the topmost level in the objects - in above example, these are the main keys (mainKey1
, etc) and that their content is dynamic (some others have deeply nested object inside and some not so.
Help!
Share Improve this question asked Apr 28, 2020 at 2:00 kzaiwokzaiwo 1,7483 gold badges21 silver badges62 bronze badges 3- You need to find key of individual object, not the complete array, loop over the array using some array method and the for each individual element get the keys and test it against your desired key – Code Maniac Commented Apr 28, 2020 at 2:04
- Do you want to extract all keys in an array ? – Ala Eddine Menai Commented May 1, 2020 at 21:15
- You get 0,1,2because you passed an array, not an object. – Ala Eddine Menai Commented May 1, 2020 at 21:25
6 Answers
Reset to default 13You can try using array.some()
:
let checkKeyPresenceInArray = key => arrOfObj.some(obj => Object.keys(obj).includes(key));
let arrOfObj = [{
"mainKey1": {
"subKey1": {
"innerKey1": {
"innerMostKey1": {
"key1": "value"
}
}
}
}
}, {
"mainKey2": {
"key2": "value"
}
}, {
"mainKey3": {
"subKey3": {
"key3": "value"
}
}
}
]
let checkKeyPresenceInArray = key => arrOfObj.some(obj => Object.keys(obj).includes(key));
var isKeyPresent = checkKeyPresenceInArray('mainKey3')
console.log(isKeyPresent);
You can iterate through the array, check and see if any of the objects has the key that you are looking for, and return true if it does. If you don't find the key, then the for
loop will complete and it will return false.
arrOfObj = [{
"mainKey1": {
"subKey1": {
"innerKey1": {
"innerMostKey1": {
"key1": "value"
}
}
}
}
}, {
"mainKey2": {
"key2": "value"
}
}, {
"mainKey3": {
"subKey3": {
"key3": "value"
}
}
}
]
function arrayHasKey(arr, key) {
for (const obj of arr) {
if (key in obj) { return true; }
}
return false;
}
console.log(arrayHasKey(arrOfObj, "mainKey2"))
console.log(arrayHasKey(arrOfObj, "mainKey10"))
this will work,it returns boolean value:
arrOfObj.hasOwnProperty('mainKey3');
You can use some
with hasOwnProperty
like this :
let checkKeyPresenceInArray = (key) => arrOfObj.some((o) => o.hasOwnProperty(key));
You have to use hasOwnProperty
method to check if the key is available in the objects in that array -
var c = 0;
arrOfObj.forEach(e => {
if(e.hasOwnProperty("mainKey1")){
c++;
}
});
if(c > 0 && c == arrOfObj.length){
console.log("The key is found in all the objects in the array.");
}
else if(c == 0){
console.log("The key is not found in any objects in the array");
}
else{
console.log("The key is found in some objects in the array");
}
It is possibly to apply JSON.stringify()
for that purpose:
let list = [{
"data1": {
"subKey1": {
"innerKey1": {
"innerMostKey1": {
"key1": "value"
}
}
}
}
}, {
"data2": {
"key2": "value"
}
}, {
"data3": {
"subKey3": {
"key3": "value"
}
}
}
]
let checkIfInArray = key => list.some(obj => JSON.stringify(obj).indexOf(key) > -1);
var result = checkIfInArray('key2')
alert(result);
本文标签: javascriptCheck if array of objects contain certain keyStack Overflow
版权声明:本文标题:javascript - Check if array of objects contain certain key - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738497597a2090076.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论