Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
97
rated 0 times [  98] [ 1]  / answers: 1 / hits: 37424  / 8 Years ago, wed, november 2, 2016, 12:00:00

I have array of different objects which look like this:



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


I want to filter the one element like status and then count the filtered, for example if status is false then return 1.



I have tried the below code but I am not sure what I am doing here:



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

More From » arrays

 Answers
2

Well, you could just do a count, or you could run a filter and get the length of the final array.



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

[#60207] Monday, October 31, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sonja

Total Points: 541
Total Questions: 113
Total Answers: 114

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
sonja questions
Mon, Nov 30, 20, 00:00, 4 Years ago
Sun, Oct 11, 20, 00:00, 4 Years ago
Thu, May 21, 20, 00:00, 4 Years ago
Sun, Nov 10, 19, 00:00, 5 Years ago
Mon, Aug 26, 19, 00:00, 5 Years ago
;