The reduce () method takes a function as an accumulator, and each value (from left to right) in the array begins to shrink, resulting in a value
Parameters
Callback
A function that executes each value in the array, containing four parameters
Previousvalue
The value returned by the last call to the callback, or the provided initial value (InitialValue)
CurrentValue
Elements in an array that are handled in a single sign
Inde
The index of the current element in the book
Array
Array that calls reduce
InitialValue
As the first argument to call Calback for the first time
Demo1:
[0,1,2,3,4].reduce (function (Previousvalue,currenvalue,index,array) {
return previousvalue+currenvalue;
})//result 10
Demo2:
[0,1,2,3,4].reduce (function (Previousvalue,currenvalue,index,array) {
return previousvalue+currenvalue;
},10)//Result 20
Reduce () usage