Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!