【问题标题】:Filter Array of objects and count the filtered elements in Javascript过滤对象数组并计算 Javascript 中过滤后的元素
【发布时间】:2016-11-02 15:09:14
【问题描述】:

我有一系列不同的对象,如下所示:

[{
   color:'red',
   'type':'2',
   'status':'true'
 }
 {
   color:'red',
   'type':'2',
   'status':'false'
 }]

我想过滤像status这样的一个元素,然后对过滤后的元素进行计数,例如如果状态为假则返回1。

我已经尝试了下面的代码,但我不确定我在这里做什么:

for (i = 0; i < check.length; i++) {
  var check2;

  console.log(check[i].isApproved);
  (function(check2) {
    return check2 = check.filter(function(val) { 
        return val == false 
    }).length;
  })(check2)

  console.log('again Rides',check2);
}

【问题讨论】:

  • 需要进一步解释。 “过滤”这个词被使用得太随意了。这很不清楚......

标签: javascript arrays filter


【解决方案1】:

如果我理解正确,您想计算status 等于'false' 的元素数量注意status 中的值是字符串

var check = [
  { color:'red', 'type':'2', 'status':'true' }, 
  { color:'red', 'type':'2', 'status':'false' } 
];

var countfiltered = check.filter(function(element){
    return element.status == 'false';
}).length

console.log(countfiltered);

【讨论】:

    【解决方案2】:

    使用 reduce 是完美的例子:

    const items = [
      { color:'red', type:'2', status:'true' }, 
      { color:'red', type:'2', status:'false' } 
    ];
    
    const falseNb = items.reduce((n, e) => e.status === 'false' ? n+1 : n, 0);
    
    console.log(falseNb) // => 1
    
    

    const items = [
      { color:'red', type:'2', status:'true' }, 
      { color:'red', type:'2', status:'false' } ,
      { color:'red', type:'2', status:'false' } ,
      { color:'green', type:'1', status:'false' } ,
      { color:'red', type:'1', status:'true' } 
    ];
    
    const falseNb = items.reduce((n, e) => e.status === 'false' ? n+1 : n, 0);
    
    document.getElementById('result').innerHTML = falseNb
    body { background: #333; color: #0f0; font-family: monospace}
    <pre>
    [
      { color:'red', type:'2', status:'true' }, 
      { color:'red', type:'2', status:'false' } ,
      { color:'red', type:'2', status:'false' } ,
      { color:'green', type:'1', status:'false' } ,
      { color:'red', type:'1', status:'true' } 
    ]
    </pre>
    Result => <span id='result'></span>

    替代方案是:

    const falseNb = items.filter(e => e.status === 'false').length;
    

    【讨论】:

      【解决方案3】:

      嗯,你可以只做一个计数,或者你可以运行一个过滤器并得到最终数组的长度。

      var count = 0;
      var arr = [{color:'red', type:'2', status:'true'},
                 {color:'red', type:'2', status:'false'} ];
      // Showing filterin to be robust. You could just do this in 
      // a loop, which would be sensible if you didn't need the subarray. 
      var filtered = arr.filter ( function ( d ) {
          // Note that I'm testing for a string, not a boolean, because
          // you are using strings as values in your objects. 
          // If it was a boolean, you'd use if ( d.status ) { ... }
          count++;
          return d.status === 'false';
      });
      
      // These should be the same, reflecting number of objs with 'false'
      console.log ( count );
      console.log ( filtered.length );
      // This should trace out a sub array of objs with status === 'false'
      console.log ( filtered );
      

      【讨论】:

        猜你喜欢
        • 2019-01-22
        • 1970-01-01
        • 2023-03-12
        • 2012-11-15
        • 1970-01-01
        • 1970-01-01
        • 2021-10-28
        • 2019-11-23
        相关资源
        最近更新 更多