javascript sum of number in object array 1

javascript sum of number in object array

arr = [{x:1}, {x:3}]

arr.reduce((accumulator, current) => accumulator + current.x, 0);

Here is what the above code is Doing:
1. The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.
2. The reducer function takes four arguments:
1. Accumulator (acc)
2. Current Value (cur)
3. Current Index (idx)
4. Source Array (src)
3. Your reducer function’s returned value is assigned to the accumulator, whose value is remembered across each iteration throughout the array, and ultimately becomes the final, single resulting value.

Similar Posts