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

252
Visualizações
How check an object's every key and value if null or undefined with ES6?

I'm trying to check an objects every entry's key and value in a if condition, but i cannot understand my result. What is the problem? Why is an undefined not false with "??" ?

const object = {undefined: "foo", B: "Bazz"}
console.log(Object.entries(object).every(([key, value]) => !!key && !!value))

//true

const object2 = {undefined: null, B: "Bazz"}
console.log(Object.entries(object2).every(([key, value]) => !!key && !!value)) 

//false
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You cannot actually set the key to undefined. When you do { undefined: "foo" }, you are actually setting the key to the string "undefined". It is essentially the same as { "undefined": "foo" }

!!"undefined" && !!"foo" => true && true => true because non-empty strings are truthy

!!"undefined" && !!null => true && false => false because null is falsy

about 4 years ago · Juan Pablo Isaza Relatório

0

The propertie names of objects are strings or Symbol. To check the value, you could take an array of unwanted strings and check with includes.

const
    check = object => Object
        .entries(object)
        .every(([key, value]) => !['', 'null', 'undefined'].includes(key) && value);

console.log(check({ a: "foo", B: "Bazz" }));
console.log(check({ undefined: "foo", B: "Bazz" }));
console.log(check({ undefined: null, B: "Bazz" }));

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