Ecmascript 5 also adds two methods to merge Arrays: reduce () and reduceright (). Both methods overlap.
All the items in the algebra group, and then construct a final returned value. The reduce () method starts from the first entry of the array and traverses one by one.
To the end. Reduceright () starts from the last entry of the array and traverses forward to the first entry.
Both methods receive two parameters: a function called on each item and (optional) the initial value of the basic value of the merge. Upload
The reduce () and reduceright () functions receive four parameters: the index of the previous value, current value, and item, and the array object. This
Any value returned by a function is automatically passed to the next parameter as the first parameter. The first iteration occurs in the second entry of the array.
One parameter is the first item of the array, and the second parameter is the second item of the array.
The reduce () method can be used to calculate the sum of all values in the array, for example:
VaR values = [1, 2, 4, 5]; var sum = values. reduce (function (prev, cur, index, array) {return Prev + cur;}); alert (SUM); // 15
S1: array Iteration Method