1var objs = [
2 {name: "Peter", age: 35},
3 {name: "John", age: 27},
4 {name: "Jake", age: 28}
5];
6
7objs.reduce(function(accumulator, currentValue) {
8 return accumulator + currentValue.age;
9}, 0); // 35 + 27 + 28 = 90
1var array = [36, 25, 6, 15];
2
3array.reduce(function(accumulator, currentValue) {
4 return accumulator + currentValue;
5}, 0); // 36 + 25 + 6 + 15 = 82
1
2[3, 2.1, 5, 8].reduce((total, number) => total + number, 0)
3
4// loop 1: 0 + 3
5// loop 2: 3 + 2.1
6// loop 3: 5.1 + 5
7// loop 4: 10.1 + 8
8// returns 18.1
9
10
1const sum = array.reduce((accumulator, element) => {
2 return accumulator + element;
3}, 0);
4// An example that will loop through an array adding
5// each element to an accumulator and returning it
6// The 0 at the end initializes accumulator to start at 0
7// If array is [2, 4, 6], the returned value in sum
8// will be 12 (0 + 2 + 4 + 6)
9
10const product = array.reduce((accumulator, element) => {
11 return accumulator * element;
12}, 1);
13// Multiply all elements in array and return the total
14// Initialize accumulator to start at 1
15// If array is [2, 4, 6], the returned value in product
16// will be 48 (1 * 2 * 4 * 6)
1array.reduce(function(acumulador, elementoAtual, indexAtual, arrayOriginal), valorInicial)