Javascript Array myReduce(reducer, initValue)

Description

Javascript Array myReduce(reducer, initValue)


Array.prototype.myReduce = function(reducer, initValue){
    const arr = this/* ww w  .  ja va2 s. c  o m*/
    function reduce(prev, index){
        const next = arr[index]
        const nextValue = reducer(prev, next)
        return index < arr.length ? reduce(nextValue, index + 1) : prev
    }
    
    return reduce(initValue, 0)
}

const arr = [1,2,3,4,5]

function reducer(a,b){
    return a+b;
}

console.log(arr.myReduce(reducer, 0))



PreviousNext

Related