Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda