Syntax:
var resultado = arr.reduce(funcion[, valorInicial]);
The reduce()
method applies a function to an accumulator and to each value of an array (from left to right) to reduce it to a single value.
.reduce parameters:
Function to execute for each value of the array, which receives four parameters:
Function parameters:
The value returned (returned) in the previous call of the function, or
valorInicial
, if provided. (See below.)
Current element that is being processed in the array.
Index of the current element that is being processed in the array.
The array on which the method was called reduces.
Object to be used as the first argument in the first call of the function.
Practical example:
[0,1,2,3,4].reduce(function(valorAnterior, valorActual, indice, vector){
return valorAnterior + valorActual;
});
// Primera llamada
valorAnterior = 0, valorActual = 1, indice = 1
// Segunda llamada
valorAnterior = 1, valorActual = 2, indice = 2
// Tercera llamada
valorAnterior = 3, valorActual = 3, indice = 3
// Cuarta llamada
valorAnterior = 6, valorActual = 4, indice = 4
// el array sobre el que se llama a reduce siempre es el objeto [0,1,2,3,4]
// Valor Devuelto: 10
With initial value:
[0,1,2,3,4].reduce(function(valorAnterior, valorActual, indice, vector){
return valorAnterior + valorActual;
}, 10);
// Primera llamada
valorAnterior = 10, valorActual = 0, indice = 0
// Segunda llamada
valorAnterior = 10, valorActual = 1, indice = 1
// Tercera llamada
valorAnterior = 11, valorActual = 2, indice = 2
// Cuarta llamada
valorAnterior = 13, valorActual = 3, indice = 3
// Quinta llamada
valorAnterior = 16, valorActual = 4, indice = 4
// el array sobre el que se llama a reduce siempre es el objeto [0,1,2,3,4]
// Valor Devuelto: 20
F Source and more examples here