Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Count array objects that have a value

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.

like image 597
John Avatar asked Feb 18 '19 17:02

John


People also ask

How do you count the number of array elements matching a condition?

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.

How do you count objects in array?

count the number of elements in an array javascript var len= arr. length; //Now arr. length returns 5. Basically, len=5.


Video Answer


5 Answers

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)
like image 193
stever Avatar answered Oct 18 '22 04:10

stever


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)
like image 37
adiga Avatar answered Oct 18 '22 03:10

adiga


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)
like image 37
Get Off My Lawn Avatar answered Oct 18 '22 04:10

Get Off My Lawn


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);
like image 1
Aniket G Avatar answered Oct 18 '22 03:10

Aniket G


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.

like image 1
Willy David Jr Avatar answered Oct 18 '22 04:10

Willy David Jr