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
  • 26,331
  • 9
  • 49
  • 67
  • 1
    The answers seem to try and reinvent [How do I check if an object has a specific property in JavaScript?](https://stackoverflow.com/q/135448) 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 Jun 07 '22 at 07:48

4 Answers4

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);
Jax-p
  • 7,225
  • 4
  • 28
  • 58
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))
RenaudC5
  • 3,553
  • 1
  • 11
  • 29
  • `{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. – VLAZ Jun 07 '22 at 07: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
Nick Vu
  • 14,512
  • 4
  • 21
  • 31
  • 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 Jun 07 '22 at 07: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
Ben Aston
  • 53,718
  • 65
  • 205
  • 331