Name
Array.reduceRight() — reduce an array from right-to-left
Availability
ECMAScript 5
Synopsis
array.reduceRight(f)array.reduceRight(f,initial)
Arguments
fA function that combines two values (such as two array elements) and returns a new “reduced” value.
initialAn optional initial value to seed the array reduction with. If this argument is specified,
reduceRight()behaves as if it had been inserted at the end ofarray.
Returns
The reduced value of the array, which is the return value of
the last invocation of f.
Description
reduceRight() works like
the reduce() method: it invokes
the function f n-1 times to reduce the n elements of
array to a single value. reduceRight() differs from reduce() only in that it enumerates array
elements from right to left (from highest index to lowest) rather
than left to right. See Array.reduce() for
details.
Example
[2,10,60].reduceRight(function(x,y){returnx/y})// => 3: (60/10)/2
See Also
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access