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

203
Visualizações
How to optimise a condition which checks for each object of an array of objects whether specific properties are not undefined?

I want code optimisation for an if-statement because I otherwise have to add more key-value pairs into this condition with &&. I have an array of objects and the condition as with the following code example.

let arr = [{
  a: 12,
  b: 14,
  c: undefined,
  d: undefined,
  e: 56,
  f: "file 1",
  g: "file 2",
  h: undefined,
}];

for (const item of arr) {
  if (
    item.a !== undefined &&
    item.b !== undefined &&
    item.c !== undefined &&
    item.d !== undefined &&
    item.e !== undefined &&
    item.f !== undefined &&
    item.g !== undefined
  ) {
    console.log("code works");
  } else {
    console.log("fails");
  }
}

I'm trying to optimise the above code, and I appreciative any suggestion.

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

0

You can use Object.values() and some() to write a shorter code. This is not a huge optimization

let arr = [{
    a:12,
    b:14,
    c:undefined,
    d:undefined,
    e:56,
    f:"file 1",
    g:"file 2",
    h:undefined
}]
for(let key of arr){
if(Object.values(key).some((x) => x===undefined)){
console.log("fails")
}else{
console.log("code works")
}
}

about 4 years ago · Juan Pablo Isaza Relatório

0

Create a function that accepts an object as an argument, gets its values, and then use some to check if any of the values are undefined. some returns after finding the first match to the condition.

const arr=[{a:12,b:14,c:undefined,d:undefined,e:56,f:'file 1',g:'file 2',h:undefined},{a:12,b:14,c:3,d:'bob from accounting',e:56,f:'file 1',g:'file 2',h:23},{a:12,b:14,c:3,d:'bob from accounting',e:56,f:'file 1',g:'file 2',h:undefined}];

function test(obj) {
  const values = Object.values(obj);
  return values.some(p => p === undefined);
}

const out = arr.map((obj, i) => {
  return `Code ${test(arr[i]) ? 'fails' : 'works'}`;
});

console.log(out);

about 4 years ago · Juan Pablo Isaza Relatório

0

If I understand the requirement correctly, You have dynamic properties in an object and you want to check if all the properties in an object having values. If Yes,

Try this :

let arr = [{
    a:12,
    b:14,
    c:undefined,
    d:undefined,
    e:56,
    f:"file 1",
    g:"file 2",
    h:undefined
}];

arr.forEach(obj => {
  let str = '';
    Object.keys(obj).forEach(key => {
    str = obj[key] ? 'code works' : 'fails'
  })
  console.log(str);
});

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