stackoverflow array reduce

Solutions on MaxInterview for stackoverflow array reduce by the best coders in the world

showing results for - "stackoverflow array reduce"
Hanna
10 Oct 2019
1const arr = [{x:1}, {x:2}, {x:4}];
2arr.reduce(function (acc, obj) { 
3    return acc + obj.x; 
4  }, 0); //result = 7
5
6// acc = accumulator (result from the last iteration)
7// obj = iterated object from arr
8// 0 = initial value for the accumulator(acc)
9
10// Arrow function =>
11arr.reduce((acc, obj) => acc + obj.x, 0);
12
13// 0 + 1 = 1 
14// 1 + 2 = 3 
15// 3 + 4 = 7;
16// *note when multiplying use a initial value of 1 
17// beceause => (0 * 2 = 0)
18
19