kzaiwo
kzaiwo

Reputation: 1748

Check if array of objects contain certain key

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!

Upvotes: 10

Views: 26782

Answers (6)

johannesMatevosyan
johannesMatevosyan

Reputation: 2208

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

Upvotes: 0

Arnab_Datta
Arnab_Datta

Reputation: 662

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

Upvotes: 0

Ala Eddine Menai
Ala Eddine Menai

Reputation: 2870

You can use some with hasOwnProperty like this :

let checkKeyPresenceInArray = (key) => arrOfObj.some((o) => o.hasOwnProperty(key));

Upvotes: 0

Sohan Patil
Sohan Patil

Reputation: 170

this will work,it returns boolean value:

arrOfObj.hasOwnProperty('mainKey3');

Upvotes: 0

Jordan Soltman
Jordan Soltman

Reputation: 3883

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

Upvotes: 1

mickl
mickl

Reputation: 49945

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

Upvotes: 13

Related Questions