reduceRight()
is reduction method.
reduceRight()
iterates all items and build up a value for return.
reduceRight()
starts at the last and travels toward the first.
reduceRight()
accepts two arguments.
The function passed into reduceRight()
accepts four arguments.
The returned value from the function is passed in as the first argument for the next item. The first iteration occurs on the second item in the array.
The reduceRight()
method works in the same way, just in the opposite direction.
var values = [1,2,3,4,5];
var sum = values.reduceRight(function(prev, cur, index, array){
return prev + cur;
});
console.log(sum); //15
The code above generates the following result.