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.
To count the elements in an array that match a condition: Use the filter() method to iterate over the array. On each iteration, check if the condition is met. Access the length property on the array to get the number of elements that match the condition.
count the number of elements in an array javascript var len= arr. length; //Now arr. length returns 5. Basically, len=5.
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 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 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)
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With