Summary
Apply a function against an accumulator and each value of the array (from right-to-left) as to reduce it to a single value.
Method of Array |
|
|---|---|
| Implemented in | JavaScript 1.8 |
| ECMAScript Edition | ECMAScript 5th Edition |
Syntax
array.reduceRight(callback[, initialValue])
Parameters
-
callback - Function to execute on each value in the array.
-
initialValue -
Object to use as the first argument to the first call of the
callback.
Description
reduceRight executes the callback function once for each element present in the array, excluding holes in the array, receiving four arguments: the initial value (or value from the previous callback call), the value of the current element, the current index, and the array over which iteration is occurring.
The call to the reduceRight callback would look something like this:
array.reduceRight(function(previousValue, currentValue, index, array) {
// ...
});
The first time the function is called, the previousValue and currentValue can be one of two values. If an initialValue was provided in the call to reduceRight, then previousValue will be equal to initialValue and currentValue will be equal to the last value in the array. If no initialValue was provided, then previousValue will be equal to the last value in the array and currentValue will be equal to the second-to-last value.
Some example run-throughs of the function would look like this:
[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {
return previousValue + currentValue;
});
// First call
previousValue = 4, currentValue = 3, index = 3
// Second call
previousValue = 7, currentValue = 2, index = 2
// Third call
previousValue = 9, currentValue = 1, index = 1
// Fourth call
previousValue = 10, currentValue = 0, index = 0
// array is always the object [0,1,2,3,4] upon which reduceRight was called
// Return Value: 10
And if you were to provide an initialValue, the result would look like this:
[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {
return previousValue + currentValue;
}, 10);
// First call
previousValue = 10, currentValue = 4, index = 4
// Second call
previousValue = 14, currentValue = 3, index = 3
// Third call
previousValue = 17, currentValue = 2, index = 2
// Fourth call
previousValue = 19, currentValue = 1, index = 1
// Fifth call
previousValue = 20, currentValue = 0, index = 0
// array is always the object [0,1,2,3,4] upon which reduceRight was called
// Return Value: 20
Compatibility
reduceRight is a recent addition to the ECMA-262 standard; as such it may not be present in other implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of reduceRight in implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming Object and TypeError have their original value and that callbackfn.call evaluates to the original value of Function.prototype.call.
if ('function' !== typeof Array.prototype.reduceRight) {
Array.prototype.reduceRight = function(callback, opt_initialValue) {
'use strict';
if (null === this || 'undefined' === typeof this) {
// At the moment all modern browsers, that support strict mode, have
// native implementation of Array.prototype.reduceRight. For instance,
// IE8 does not support strict mode, so this check is actually useless.
throw new TypeError(
'Array.prototype.reduceRight called on null or undefined');
}
if ('function' !== typeof callback) {
throw new TypeError(callback + ' is not a function');
}
var index, value,
length = this.length >>> 0,
isValueSet = false;
if (1 < arguments.length) {
value = opt_initialValue;
isValueSet = true;
}
for (index = length - 1; -1 < index; --index) {
if (!this.hasOwnProperty(index)) {
if (isValueSet) {
value = callback(value, this[index], index, this);
}
else {
value = this[index];
isValueSet = true;
}
}
}
if (!isValueSet) {
throw new TypeError('Reduce of empty array with no initial value');
}
return value;
};
}
Examples
Example: Sum up all values within an array
var total = [0, 1, 2, 3].reduceRight(function(a, b) {
return a + b;
});
// total == 6
Example: Flatten an array of arrays
var flattened = [[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) {
return a.concat(b);
}, []);
// flattened is [4, 5, 2, 3, 0, 1]
Browser compatibility
Based on Kangax's compat tables
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | 3.0 (1.9) | 9 | 10.5 | 4.0 |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | ? | ? | ? | ? | ? | ? |
Mozilla Developer Network