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

140
Vistas
Is there a way to check for falsy values from an array?

I want to check a bunch of variables for falsy values.

Instead of writing a very long if statement, e.g.:

if (!userData.email || !userData.lastName || ! userData.firstName etc...

I wanted to put these values in an array and simply check the array, e.g.:

      var detailsConditionsArr = [
        userData.firstName,
        userData.lastName,
        userData.email,
        userData.phone,
        userData.address,
        userData.mailAddress,
      ]

      if (detailsConditionsArr.indexOf(false) === 1) {
        console.log("Found a falsy");
      }

But the above doesn't work (if statement never fires).

The values could be false, null or ''.

Is there a way to check these from the array?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Ideal use case for Array#some

Check for falsy value in the iteration, if the test succeeds, true will be returned.

var A = ["Name", ""];
var B = ["Name", "30"];
var C = ["Name", "30", null];

if (A.some(el => !el)) {
  console.log("Found a falsy in A");
}

if (B.some(el => !el)) {
  console.log("Found a falsy in B");
}

if (C.some(el => !el)) {
  console.log("Found a falsy in C");
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use the key names you want to check as an array, then do the check like this:

const userData = { email: '', lastName: '', firstName: '' };
const hasFalsy = ['email', 'lastName', 'firstName'].some(k => !Boolean(userData[k]));

if (hasFalsy) {
  console.log('found a falsy');
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use reduce to return a single value from the array like so:

var detailsConditionsArr = [
    userData.firstName,
    userData.lastName,
    userData.email,
    userData.phone,
    userData.address,
    userData.mailAddress,
  ]

if (detailsConditionsArr.reduce((carry, el)=> el ? carry : true, false)) {
  console.log("Found a falsy");
}
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