Javascript - Count array objects that have a value

2020-04-11 07:57发布

问题:

I'm getting an array returned to me and I need to count the rows that have a value in them

I've tried to call arr.length but that gives me the total length of the array

var arr = [
  { id: '1', '': '' },
  { id: '2', '': '' },
  { id: '3', '': '' },
  { id: '4', '': '' },
  { id: '', '': '' },
  { id: '', '': '' },
  { id: '', '': '' }
]

The result should be 4.

回答1:

Use filter to only capture keys with value, and return the resulting array's length.

var arr = [ { id: '1', '': '' },{ id: '2', '': '' },{ id: '3', '': '' },{ id: '4', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' } ]

var res = arr.filter(val => {
    return val.id
})
console.log(res.length)



回答2:

Foreach through the array and check if the id key has a value

var output = 0;
var arr = [ { id: '1', '': '' },{ id: '2', '': '' },{ id: '3', '': '' },{ id: '4', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' } ]

arr.forEach(e => {
  if (e["id"] != '') {
    output++;
  }
});

console.log(output);



回答3:

You can try this:

var count = 0;
for (var x of arr) {
  if (x.id != '') {
    count++;
  }
}

Basically what I do is that loop through all the object and if it's not an empty string, then count it.



回答4:

You can use reduce and some like this. This checks for all the keys in the object:

const arr = [{id:'1','':''},{id:'2','':''},{id:'3','':''},{id:'4','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''}]

const count = arr.reduce((r,a) => Object.values(a).some(v => v) ? ++r: r, 0)
console.log(count)



回答5:

Use reduce to add the items up if id.length > 0

var arr = [ { id: '1', '': '' },
{ id: '2', '': '' },
{ id: '3', '': '' },
{ id: '4', '': '' },
{ id: '', '': '' },
{ id: '', '': '' },
{ id: '', '': '' },
{ id: '', '': '' },
{ id: '', '': '' },
{ id: '', '': '' },
{ id: '', '': '' },
{ id: '', '': '' },
{ id: '', '': '' },
{ id: '', '': '' },
{ id: '', '': '' },
{ id: '', '': '' },
{ id: '', '': '' },
{ id: '', '': '' },
{ id: '', '': '' },
{ id: '', '': '' },
{ id: '', '': '' } ]


let count = arr.reduce((val, itm) => itm.id.length > 0 ? val + 1 : val + 0, 0)

console.log(count)



回答6:

You could count if a row has a truthy value.

var array = [ { id: '1', '': '' }, { id: '2', '': '' }, { id: '3', '': '' }, { id: '4', '': '' }, { id: '', '': '' }, { id: '', '': '' }, { id: '', '': '' }, { id: '', '': '' }, { id: '', '': '' }, { id: '', '': '' }, { id: '', '': '' }, { id: '', '': '' }, { id: '', '': '' }, { id: '', '': '' }, { id: '', '': '' }, { id: '', '': '' }, { id: '', '': '' }, { id: '', '': '' }, { id: '', '': '' }, { id: '', '': '' }, { id: '', '': '' }],
    count = array.reduce((s, o) => s + Object.values(o).some(Boolean), 0);
    
console.log(count);

List item



回答7:

Using reduce

var arr = [ { id: '1', '': '' },{ id: '2', '': '' },{ id: '3', '': '' },{ id: '4', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' },{ id: '', '': '' } ]

console.log(arr.reduce((acc,e)=>{e.id!=''?acc++:false; return acc},0))



回答8:

if you are a fan of Lodash, you can use countBy (documentation)

const array = [{id:'1','':''},{id:'2','':''},{id:'3','':''},{id:'4','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''},{id:'','':''}]

// The iterator function `obj => obj.id === ''` returns Boolean value `true` or `false`
const count = _.countBy(array, obj => obj.id !== '');

// Get the `true` value
console.log(count.true);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

One liner fans:

_.countBy(array, obj => obj.id !== '').true;