Merger Approach


All, a merger eventually return value reduce () and reduceRight (), these two methods are iterative array, reduceRight () from the array of the last item, traversing forward to the first item,

Both parameters are received four
first: the previous value of
the second: the current value of
the index entry: Third
Fourth: an array of objects

var values = [1,2,3,4,5]; 
var sum = values.reduce(function(prev, cur, index, array){ 
return prev + cur; 
}); //1+2+3+4+5
console.log(sum); //15

var values = [1,2,3,4,5]; 
var sum = values.reduceRight(function(prev, cur, index, array){ 
return prev + cur; 
}); //5+4+3+2+1
console.log(sum); //15

  

Guess you like

Origin www.cnblogs.com/hongding/p/11019692.html
Recommended