Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

202
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda