Javascript - Count array objects that have a value

后端 未结 8 598
情话喂你
情话喂你 2021-01-19 01:52

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

相关标签:
8条回答
  • 2021-01-19 02:34

    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

    0 讨论(0)
  • 2021-01-19 02:36

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

    0 讨论(0)
  • 2021-01-19 02:37

    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)

    0 讨论(0)
  • 2021-01-19 02:40

    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)

    0 讨论(0)
  • 2021-01-19 02:43

    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.

    0 讨论(0)
  • 2021-01-19 02:44

    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)

    0 讨论(0)
提交回复
热议问题