过滤对象数组并计算Javascript中过滤的元素

DEO*_*DEO 4 javascript arrays filter

我有一系列不同的对象,它们看起来像这样:

[{
   color:'red',
   'type':'2',
   'status':'true'
 }
 {
   color:'red',
   'type':'2',
   'status':'false'
 }]
Run Code Online (Sandbox Code Playgroud)

我想过滤一个元素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);
}
Run Code Online (Sandbox Code Playgroud)

tag*_*izy 13

如果我理解正确,你想计算元素的数量,其中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);
Run Code Online (Sandbox Code Playgroud)


538*_*MEO 9

给定这个数据集:

const items = [
  { color:'red', type:'2', status:'true' }, 
  { color:'red', type:'2', status:'false' } 
];
Run Code Online (Sandbox Code Playgroud)

这里有两种方法来解决这个问题:

使用减少

const falseNb = items.reduce((n, item) => item.status === 'false' ? n+1 : n, 0)
Run Code Online (Sandbox Code Playgroud)

使用过滤器

const falseNb = items.filter(e => e.status === 'false').length;
Run Code Online (Sandbox Code Playgroud)

请注意,根据此测试reduce 方法的性能提高了一倍。

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('result1').innerHTML = falseNb

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

document.getElementById('result2').innerHTML = falseNb
Run Code Online (Sandbox Code Playgroud)
body { background: #333; color: #0f0; font-family: monospace}
Run Code Online (Sandbox Code Playgroud)
<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 with Reduce => <span id='result1'></span><br><br>

Result with Filter => <span id='result2'></span>
Run Code Online (Sandbox Code Playgroud)


Tim*_*zio 4

好吧,您可以只进行计数,或者可以运行过滤器并获取最终数组的长度。

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 );
Run Code Online (Sandbox Code Playgroud)