Reduce to sum all the values ​​in the array of the object failed

As a result, I got it [object Object]9778177, I tried to parse this value, but nothing helps, something is wrong.

let x = [{
  "total_count": 7
}, {
  "total_count": 9
}, {
  "total_count": 778
}, {
  "total_count": 177
}]

let sum = x.reduce((accum, obj) => {
   return accum + obj.total_count
})

console.log(sum)
+4
source share
1 answer

You can add an initial value because the first iteration starts with the object for the battery and there you do not have the property you will need.

let sum = x.reduce((accum, obj) => accum + obj.total_count, 0);
+5
source

Source: https://habr.com/ru/post/1683499/


All Articles