|
In continuation of series of ECMAScript 5.
This article describes reduceRight
feature of ECMAScript 5. It generates single result by triggering callbackfn
to each array element in reverse order (Right to Left). The result of
previous callbackfn is passed to the current callbackfn. The result of the
final call is the value returned by reduceRight method. If array has n
element, the reduceRight will invoke callbackfn n-1 times.
Array.reduceRight()
Signature
array.reduceRight(callbackfn [, initialValue])
callbackfn - It tests array elements.
callbackfn should be defined as function (previousValue[,index
[,array]]).
initialValue - initialValue is optional. If it
is present, its act as previousValue and passed to callbackfn as argument. If
initialValue is not present reduceRight uses the first element of array as
previousValue in callbackfn.
Returns
reduceRight returns the last invocation value of
callbackfn.
Example
var arr = [2, 10, 80];
arr.reduceRight(function(previousValue, value) {
return previousValue / value; }) Output: 4
This ends the reduceRight
feature of array in ECMAScript 5.
|