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

130
Vistas
Count null property values

I have a simple for loop below where I'm going through a specific property that has null values. I want to count the total of the nulls and store those in a variable, which should be 7. I keep converting the nulls to 0 or undefined.

const test = [
    {
        'theKey': null,
        'theName': 'bill'
    },
    {
        'theKey': null,
        'theName': 'jon'
    },
    {
        'theKey': null,
        'theName': 'theo'
    },
    {
        'theKey': null,
        'theName': 'rich'
    },
    {
        'theKey': null,
        'theName': 'rigo'
    },
    {
        'theKey': null,
        'theName': 'cleo'
    },
    {
        'theKey': null,
        'theName': 'jill'
    },
]

for (var i = 0; i < test.length; i++) {
    const theValues = test[i]['theKey']
    console.log(theValues)
}

I've tried...

console.log(theValues.length)

and

for (var i = 0; i < test.length; i++) {
    const theValues = test[i]['theKey']
    const theSum = _.sum(theValues)
    // console.log(theValues)

    console.log(theSum)
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Using for-loop:

const test = [ { 'theKey': null, 'theName': 'bill' }, { 'theKey': null, 'theName': 'jon' }, { 'theKey': null, 'theName': 'theo' }, { 'theKey': null, 'theName': 'rich' }, { 'theKey': null, 'theName': 'rigo' }, { 'theKey': null, 'theName': 'cleo' }, { 'theKey': null, 'theName': 'jill' } ];

let count = 0;
for (var i = 0; i < test.length; i++) {
  if (test[i]['theKey'] === null) {
    count++; 
  }
}

console.log(count);

Using Array#reduce:

const test = [ { 'theKey': null, 'theName': 'bill' }, { 'theKey': null, 'theName': 'jon' }, { 'theKey': null, 'theName': 'theo' }, { 'theKey': null, 'theName': 'rich' }, { 'theKey': null, 'theName': 'rigo' }, { 'theKey': null, 'theName': 'cleo' }, { 'theKey': null, 'theName': 'jill' } ];

const count = test.reduce((total, { theKey }) => 
  theKey === null ? total + 1 : total
, 0);

console.log(count);

about 4 years ago · Juan Pablo Isaza Denunciar

0

theValues isn't an array, it doesn't have a length. It's also local to the for loop, so you can't access it after the loop.

There's no need for an array, just increment a counter variable.

let nullCount = 0;
for (let i = 0; i < test.length; i++) {
    if (test[i].theKey === null) {
        nullCount++;
    }
}
console.log(nullCount);
about 4 years ago · Juan Pablo Isaza Denunciar

0

const test = [
    {
        'theKey': null,
        'theName': 'bill'
    },
    {
        'theKey': null,
        'theName': 'jon'
    },
    {
        'theKey': null,
        'theName': 'theo'
    },
    {
        'theKey': null,
        'theName': 'rich'
    },
    {
        'theKey': null,
        'theName': 'rigo'
    },
    {
        'theKey': null,
        'theName': 'cleo'
    },
    {
        'theKey': null,
        'theName': 'jill'
    },
];

let count = 0;
for (var i = 0; i < test.length; i++) {
    if(test[i]['theKey'] == null){
    count++;
    }
}
    console.log(count);

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