find sum of Boolean values JavaScript object array

Sathya V picture Sathya V · Jan 19, 2017 · Viewed 12.2k times · Source

Hi i am trying to find the sum of Boolean values in the object array in JavaScript

My json like be

var myoBj = [{
  "id": 1,
  "day": 1,
  "status": true
}, {
  "id": 2,
  "day": 1,
  "status": false
}, {
  "id": 3,
  "day": 1,
  "status": false
}, {
  "id": 4,
  "day": 3,
  "status": false
}];

i want the sum of all status values using reduce function in JavaScript/ typescript

i want to show overall status as true only when all status are true else it should be false

Answer

Bálint picture Bálint · Jan 19, 2017
var result = myObj.reduce((sum, next) => sum && next.status, true);

This should return true, if every value is true.