0

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

VLAZ
29.6k9 gold badges65 silver badges87 bronze badges
asked Jun 7, 2022 at 7:35
1
  • 1
    The 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." Commented Jun 7, 2022 at 7:48

4 Answers 4

1

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

answered Jun 7, 2022 at 7:38
3
  • This will not shortcut if a mismatch is found? Commented Jun 7, 2022 at 7:40
  • {alert: ""} will be counted as a failure Commented Jun 7, 2022 at 7:41
  • Note this wont work with {alert: false} either Commented Jun 7, 2022 at 7:41
1

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

answered Jun 7, 2022 at 7:40
1
  • {alert: undefined} means the key is present but the value of it is set to undefined. That's different to {} where the key is not present. Commented Jun 7, 2022 at 7:42
1

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

answered Jun 7, 2022 at 7:42
1
  • 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. Commented Jun 7, 2022 at 7:43
0

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

answered Jun 7, 2022 at 7:53

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.