Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Array of objects and count the filtered elements in Javascript

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);
}
like image 737
DEO Avatar asked Nov 27 '22 20:11

DEO


1 Answers

If I understood correctly you want to count the number of elements where status is equal to 'false' note: The values you have in status are strings

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);
like image 115
taguenizy Avatar answered Nov 30 '22 09:11

taguenizy