11

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);
}
1
  • 1
    Further explanation is required. The word 'filtered' is being used too liberally. This is very unclear... Commented Nov 2, 2016 at 15:15

3 Answers 3

28

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

Sign up to request clarification or add additional context in comments.

1 Comment

Updated with newer JS features: const countfiltered = check.filter((c) => c.status === "false").length;
12

Given this dataset:

const items = [
  { color:'red', type:'2', status:'true' }, 
  { color:'red', type:'2', status:'false' } 
];

Here are two way to solve the problem:

Using reduce

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

Using filter

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

Please note that according to this test, the reduce method is twice more performant

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
body { background: #333; color: #0f0; font-family: monospace}
<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>

Comments

5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.