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

129
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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