2016-11-02 13 views
0

でフィルタ要素を数えますFilterオブジェクトの配列と、私は次のようになり、異なるオブジェクトの配列を持っているのJavascript

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); 
} 
+1

さらに説明が必要です。 「フィルタリングされた」という言葉はあまりにも自由に使用されています。これは非常に不明です... –

答えて

1

さて、あなたはカウントだけで行うことができ、あるいは、あなたは可能性がある:status'false'ノートに等しい要素の数をカウントしますフィルタを実行して、最終配列の長さを取得します。

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

ありがとう作品 – DEO

+0

感謝の気持ちで答えてください:) –

0

私が正しくあなたを理解している場合:状況は、私は以下のコードを試してみましたが、私は私がここでやっているのかわからないです。1.

を返し、その後偽であります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);

関連する問題