0

How to sum of values in an object array in JavaScript

JavaScript
const arr = [{x: 1}, {x: 2}, {x: 3}];

const reducer = (accumulator, currentValue) => accumulator + currentValue.x;

let initialValue = 0
let sum = arr.reduce(reducer, initialValue)

console.log(sum) // logs 6
RUN
add comment
Suggest a different solution or add another example