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

133
Visualizações
javascript how to check if an array contains only empty objects

If I have a list of objects, how can I check if elements in the list are empty objects. I feel like this should be straight forward however it confuses me a little because when I do something like this in my react function component

console.log(list[0])

It will output {} on every render in my react component

However when I do something like

if (list[0] == {}){
  console.log('empty')}

It never ends up reaching there. Is there a cleaner way to check if objects inside a list are empty? Or preferably if all the objects in a list are empty? I feel like im missing something very obvious but im not sure why it wouldnt reach the log in the if conditional.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

A way in which you can check that every element in a list is using the every() method (read more here). The every method will return a boolean value of whether every element in the list passes the test implemented in the provided function.

In this case, we want to check if every object is considered empty. According to this StackOverflow answer we can test if an object is empty with the following function.

function isEmpty(val) {
  return Boolean(val && typeof val === 'object') && !Object.keys(val).length;
}

We can then implement this into the every function.

const allEmpty = myList.every(element => {
  return Boolean(element && typeof element === 'object') && !Object.keys(element).length;
});

The allEmpty variable will be true if all elements of your list are considered empty objects.

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