Consider the following data set:
const elements = [
{ color:'blue', type:'1', status:'true' },
{ color:'blue', type:'2', status:'false' }
];
Here are a couple of methods to address the issue:
Utilizing reduce
const falseCount = elements.reduce((num, element) => element.status === 'false' ? num+1 : num, 0)
Using filter approach
const falseCount = elements.filter(item => item.status === 'false').length;
Please take note that as per
const elements = [
{ 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 = elements.reduce((n, e) => e.status === 'false' ? n+1 : n, 0)
document.getElementById('result1').innerHTML = falseNb
const falseNbFilter = elements.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>