English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
reduceRight() The function is the same as the reduce() function, but the reduceRight() function adds the array items from the end of the array to the beginning.
reduceRight()The method executes the callback function once for each array index.
The return value of the function is stored in the accumulator (result).
array.reduceRight(callback, initialValue)
var nums = [[0, 1], [2, 3], [4, 5]; function fire(x, y) { return x.concat(y); } function myFunc() { document.getElementById("result").innerHTML = nums.reduceRight(fire); }Test and See‹/›
The numbers in the table specify the first browser version that fully supports the reduceRight() method:
Method | |||||
reduceRight() | is | 3 | 10.5 | 4 | 9 |
Parameter | Description |
---|---|
callback | a function that is run for each element in the array. Function parameters:
|
initialValue | (Optional) The value used as the first argument of the callback for the initial call. If an initial value is not provided, the first element of the array will be used. |
Return Value: | Reduce the number of generated values |
---|---|
JavaScript Version: | ECMAScript 5 |
Difference between reduce() and reduceRight():
var arr = ['1''2''3''4''5']; function funcReduce() { var val = arr.reduce(function(x, y) { return x + y;}); document.getElementById("result").innerHTML = val; } function funcReduceRight() { var val = arr.reduceRight(function(x, y) { return x + y;}); document.getElementById("result").innerHTML = val; }Test and See‹/›