Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
SyntaxreduceRight( callbackfn [, initialValue ])
An object that contains the accumulated result from the last call to the callback function.
ExamplesThe following example concatenates array values into a string, separating the values with "::". Because no initial value is provided to the reduceRight method, the first call to the callback function has 456 as the previousValue argument and 123 as the currentValue argument.
function appendCurrent (previousValue, currentValue) {
return previousValue + "::" + currentValue;
}
var elements = ["abc", "def", 123, 456];
var result = elements.reduceRight(appendCurrent);
document.write(result);
The following example finds the sum of the squares of the array elements. The reduceRight method is called with an initial value of 0.
function Process(previousValue, currentValue, index, array) {
var nextValue = previousValue + (currentValue * currentValue);
return nextValue;
}
var numbers = [3, 4, 5];
var sumOfSquares = numbers.reduceRight(Process, 0);
document.write("sum of squares=" + sumOfSquares);
The following example gets those elements of an array whose values are between 1 and 10. The initial value provided to the reduceRight method is an empty array.
function Process2(previousArray, currentValue) {
var nextArray;
if (currentValue >= 1 && currentValue <= 10)
nextArray = previousArray.concat(currentValue);
else
nextArray = previousArray;
return nextArray;
}
var numbers = [20, 1, -5, 6, 50, 3];
var emptyArray = new Array();
var resultArray = numbers.reduceRight(Process2, emptyArray);
document.write("result array=" + resultArray);
The reduceRight method can be applied to a string. The following example shows how to use this method to reverse the characters in a string.
function AppendToArray(previousValue, currentValue) {
return previousValue + currentValue;
}
var word = "retupmoc";
var result = [].reduceRight.call(word, AppendToArray, "the ");
document.write(result);
Remarks
If an initialValue is provided, the reduceRight method calls the callbackfn function one time for each element in the array, in descending index order. If no initialValue is provided, the reduceRight method calls the callbackfn function on each element, starting with the second-to-last element, in descending index order.
The return value of the callback function is provided as the previousValue argument on the next call to the callback function. The return value of the last call to the callback function is the return value of the reduceRight method.
The callback function is not called for missing elements of the array.
To accumulate a result in ascending index order, use the reduce Method (Array).
The syntax of the callback function is as follows:
function callbackfn(previousValue, currentValue, currentIndex, array1)
You can declare the callback function by using up to four parameters.
The following table lists the callback function parameters.
Callback argument Definition previousValue The value from the previous call to the callback function. If an initialValue is provided to the reduceRight method, the previousValue is initialValue the first time the function is called. currentValue The value of the current array element. currentIndex The numeric index of the current array element. array1 The array object that contains the element.The first time the callback function is called, the values provided as arguments depend on whether the reduceRight method has an initialValue argument.
If an initialValue is provided to the reduceRight method:
If an initialValue is not provided:
The array object can be modified by the callback function.
The following table describes the results of modifying the array object after the reduceRight method starts.
Condition after the reduceRight method starts Element passed to callback function? Element is added beyond the original length of the array. No. Element is added to fill in a missing element of the array. Yes, if that index has not yet been passed to the callback function. Element is changed. Yes, if that element has not yet been passed to the callback function. Element is deleted from the array. No, unless that element has already been passed to the callback function. ExceptionsA TypeError exception is thrown when either of the following conditions is true:
[15.4.4.22 Array.prototype.reduceRight ( callbackfn , initialValue )] ECMAScript® Language Specification Standard ECMA-262 5.1 Edition / June 2011
AttributionsMicrosoft Developer Network: Article
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4