Home > Mobile >  Count null property values
Count null property values

Time:11-18

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

CodePudding user response:

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

CodePudding user response:

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related