John
John

Reputation: 173

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.

Upvotes: 9

Views: 20333

Answers (8)

Jee Mok
Jee Mok

Reputation: 6556

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;

Upvotes: 0

Willy David Jr
Willy David Jr

Reputation: 9131

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.

Upvotes: 1

Aniket G
Aniket G

Reputation: 3512

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

Upvotes: 1

stever
stever

Reputation: 1241

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)

Upvotes: 11

ellipsis
ellipsis

Reputation: 12152

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

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386578

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

Upvotes: 0

Get Off My Lawn
Get Off My Lawn

Reputation: 36311

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)

Upvotes: 1

adiga
adiga

Reputation: 35222

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)

Upvotes: 3

Related Questions