9

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.

0

8 Answers 8

11

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)

Sign up to request clarification or add additional context in comments.

1 Comment

You could simplify the filter to look like this arr.filter(val => val.id)
3

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)

Comments

1

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)

Comments

1

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

Comments

1

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.

Comments

0

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

Comments

0

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

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.