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