Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 387
kzaiwo Avatar asked Apr 28 '20 02:04

kzaiwo


People also ask

How do you check if an array of objects has a key?

Using the Object. key generates and returns an array whose components are strings of the names (keys) of an object's properties. This may be used to loop through the object's keys, which we can then use to verify if any match a certain key in the object.

How do I check if an object contains a key?

Use the in operator to check if a key exists in an object, e.g. "key" in myObject . The in operator will return true if the key is present in the object, otherwise false is returned. Copied! The syntax used with the in operator is: string in object .

How do you filter keys in an array of objects?

JavaScript objects don't have a filter() method, you must first turn the object into an array to use array's filter() method. You can use the Object. keys() function to convert the object's keys into an array, and accumulate the filtered keys into a new object using the reduce() function as shown below.


2 Answers

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);
like image 183
mickl Avatar answered Oct 08 '22 18:10

mickl


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"))
like image 24
Jordan Soltman Avatar answered Oct 08 '22 18:10

Jordan Soltman