10

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!

asked Apr 28, 2020 at 2:00
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 Commented Apr 28, 2020 at 2:04
  • Do you want to extract all keys in an array ? Commented May 1, 2020 at 21:15
  • You get 0,1,2because you passed an array, not an object. Commented May 1, 2020 at 21:25

6 Answers 6

13

You 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);

answered Apr 28, 2020 at 2:02
1

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"))

answered Apr 28, 2020 at 2:05
0

this will work,it returns boolean value:

arrOfObj.hasOwnProperty('mainKey3');
answered May 1, 2020 at 7:24
1
  • It returns false even the key exists because it checks properties, not objects. Commented May 1, 2020 at 21:45
0

You can use some with hasOwnProperty like this :

let checkKeyPresenceInArray = (key) => arrOfObj.some((o) => o.hasOwnProperty(key));
answered May 1, 2020 at 21:51
0

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");
}
answered May 1, 2020 at 22:12
0

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);
answered Nov 16, 2022 at 11:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.