Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Check for a key inside In array of objects

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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!