Javascript Array reduce2(callback, valorInicial)

Description

Javascript Array reduce2(callback, valorInicial)


Array.prototype.reduce2 = function(callback, valorInicial){
    const indiceInicial = valorInicial ? 0 : 1
    let acumulador = valorInicial || this[0];
    for(let i = 1; i < this.length; i++){
        acumulador = callback(acumulador, this[i], i , this)
    }//from  w w  w  .  j  av a2s  .  co  m
    return acumulador;
}

const soma = (total, valor) =>total + valor;
const nuns = [1, 2, 3, 4, 5, 6]
console.log(nuns.reduce2(soma, 21))



PreviousNext

Related