I am trying to find the best way to check whether an object key is present inside multiple objects present in an array which will provide a boolean as output
[{alert:hi},{alert:bye},{}]
From the above example basically what I am trying to achieve is if any one object is missing the alert object key the output should be as false or anything
-
1The answers seem to try and reinvent How do I check if an object has a specific property in JavaScript? which has been on the site for more than a dozen years? Note the top answer by John Resig (author of jQuery): "I'm really confused by the answers that have been given - most of them are just outright incorrect. Of course you can have object properties that have undefined, null, or false values."VLAZ– VLAZ2022年06月07日 07:48:59 +00:00Commented Jun 7, 2022 at 7:48
4 Answers 4
You can iterate your array with every()
. Something like this:
const objects = [{alert:'hi'},{alert:'bye'},{}];
const every = objects.every(obj => obj.hasOwnProperty('alert'));
console.log(every);
-
This will not shortcut if a mismatch is found?Ben Aston– Ben Aston2022年06月07日 07:40:34 +00:00Commented Jun 7, 2022 at 7:40
-
{alert: ""}
will be counted as a failureVLAZ– VLAZ2022年06月07日 07:41:17 +00:00Commented Jun 7, 2022 at 7:41 -
Note this wont work with
{alert: false}
eitherRenaudC5– RenaudC52022年06月07日 07:41:56 +00:00Commented Jun 7, 2022 at 7:41
You can use the Array#some method and check if at least one element is undefined
const isAlertMissing = (array) => array.some(elem => elem.alert === undefined)
const objs1 = [{alert: "foo"},{alert: "foo"},{}]
const objs2 = [{alert: "foo"},{alert: "foo"}]
console.log(isAlertMissing(objs1))
console.log(isAlertMissing(objs2))
-
{alert: undefined}
means the key is present but the value of it is set toundefined
. That's different to{}
where the key is not present.VLAZ– VLAZ2022年06月07日 07:42:12 +00:00Commented Jun 7, 2022 at 7:42
You can use every
to check all items and some
with Object.keys
for finding a key in the inner objects.
const data = [{alert:"hi"},{alert:"bye"},{}]
const result = data.every(item => Object.keys(item).some(key => key === "alert"));
console.log(result) //false
EDIT
some
with Object.keys
is kind of roundabout, so we can use hasOwnProperty
instead.
const data = [{alert:"hi"},{alert:"bye"},{}]
const result = data.every(item => item.hasOwnProperty("alert"));
console.log(result) //false
-
2
Object.key()
is extremely roundabout way to check for a key. Moreover, it only works on own keys which rarely matters but might still be a consideration.VLAZ– VLAZ2022年06月07日 07:43:52 +00:00Commented Jun 7, 2022 at 7:43
Array#some
will succeed as soon a match is found, making it more efficient than Array#every
.
const test = (data, propName) =>
!(data.some((el) => !el.hasOwnProperty(propName)))
const data1 = [ {alert:'hi'}, {alert:'bye'}, {}]
const data2 = [ {alert:'hi'}, {alert:'bye'}]
console.log(test(data1, 'alert')) // false
console.log(test(data2, 'alert')) // true
Or:
const test = (data, propName) => {
for(let el of data) {
if(!el.hasOwnProperty(propName))
return false
}
return true
}
const data1 = [ {alert:'hi'}, {alert:'bye'}, {}]
const data2 = [ {alert:'hi'}, {alert:'bye'}]
console.log(test(data1, 'alert')) // false
console.log(test(data2, 'alert')) // true