2016-11-02 5 views
0

JavaScript로 여과 요소 카운트 :필터 오브젝트의 배열과 같이 I 다른 개체의 배열을

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

내가 status 같은 하나 개의 요소를 필터링하고, 예하는 경우에 대해, 필터링 된 카운트 할를

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

추가 설명이 필요합니다. 'filtered'라는 단어가 너무 자유롭게 사용되었습니다. 이것은 매우 불분명합니다. –

답변

1

글쎄, 당신은 수를 다만 수, 또는 당신은 할 수 필터를 실행하고 최종 배열의 길이를 가져옵니다.

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

관련 문제