翻譯不完整。請協助 翻譯此英文文件。
reduceRight()
方法將一個累加器及陣列中每一個值(由右至左)傳入回呼函式,將陣列化為單一值。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
See also Array.prototype.reduce()
for left-to-right.
語法
arr.reduceRight(callback[, initialValue])
參數
callback
- 用於處理陣列中每個值的函式,可傳入四個參數:
previousValue
- 前一次執行回呼函式所回傳的值,或是
initialValue
(若有提供的話,詳如下敘)。 currentValue
- 原陣列目前所迭代處理中的元素。
index
選擇性- 原陣列目前所迭代處理中的元素之索引。
array
選擇性- 呼叫
reduce
方法的陣列。
initialValue
選擇性- 於第一次呼叫
callback
時要傳入的累加器初始物件。若沒有提供初始值,則原陣列的最後一個元素將會被當作初始的累加器。假如於一個空陣列呼叫 reduce 方法且沒有提供累加器初始值,將會發生錯誤。
回傳值
簡化後的結果值。
描述
reduceRight
會對每一個目前迭代到的陣列元素(除了空值以外)執行回呼函式,回呼函式會接收四個參數:初始值(或前一次呼叫回呼函式的回傳值)、目前的元素值、目前的索引,以及正在迭代中的陣列。
可以如以下方式呼叫 reduceRight 的 callback
:
array.reduceRight(function(previousValue, currentValue, index, array) { // ... });
當回呼函式第一次被呼叫時,previousValue
與 currentValue
的值可能為兩種不同的狀況:若在呼叫 reduceRight
時有提供 initialValue
,則 previousValue
將會等於 initialValue
,且 currentValue
會等於陣列中的最後一個元素值;若沒有提供 initialValue
,則 previousValue
會等於陣列的最後一個元素值,且 currentValue
將會等於陣列的倒數第二個元素值。
若陣列為空且沒有提供 initialValue
,將會拋出 TypeError
。假如陣列只有一個元素(無論其索引位置為何)並且沒有提供 initialValue
,或如果提供了 initialValue
但陣列為空,則此唯一的值將會被直接回傳而不會呼叫 callback
函式。
一些可以運作的例子看起像是這樣:
[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) { return previousValue + currentValue; });
所傳入的回呼函式將被呼叫四次,所傳入的參數與回傳值如下所示:
callback |
previousValue |
currentValue |
index |
array |
return value |
---|---|---|---|---|---|
first call | 4 |
3 |
3 |
[0, 1, 2, 3, 4] |
7 |
second call | 7 |
2 |
2 |
[0, 1, 2, 3, 4] |
9 |
third call | 9 |
1 |
1 |
[0, 1, 2, 3, 4] |
10 |
fourth call | 10 |
0 |
0 |
[0, 1, 2, 3, 4] |
10 |
reduceRight
的最終回傳值將會是最後一次呼叫回呼函式的回傳值(10
)。
而假如有提供 initialValue
,結果會看起來像:
[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) { return previousValue + currentValue; }, 10);
callback |
previousValue |
currentValue |
index |
array |
return value |
---|---|---|---|---|---|
first call | 10 |
4 |
4 |
[0, 1, 2, 3, 4] |
14 |
second call | 14 |
3 |
3 |
[0, 1, 2, 3, 4] |
17 |
third call | 17 |
2 |
2 |
[0, 1, 2, 3, 4] |
19 |
fourth call | 19 |
1 |
1 |
[0, 1, 2, 3, 4] |
20 |
fifth call | 20 |
0 |
0 |
[0, 1, 2, 3, 4] |
20 |
這次 reduceRight
的最終回傳值將會是 20
。
範例
Sum up all values within an array
var sum = [0, 1, 2, 3].reduceRight(function(a, b) { return a + b; }); // sum is 6
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]
Difference between reduce
and reduceRight
var a = ['1', '2', '3', '4', '5']; var left = a.reduce(function(prev, cur) { return prev + cur; }); var right = a.reduceRight(function(prev, cur) { return prev + cur; }); console.log(left); // "12345" console.log(right); // "54321"
Polyfill
reduceRight
was added to the ECMA-262 standard in the 5th edition; as such it may not be present in all 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.
// Production steps of ECMA-262, Edition 5, 15.4.4.22 // Reference: http://es5.github.io/#x15.4.4.22 if ('function' !== typeof Array.prototype.reduceRight) { Array.prototype.reduceRight = function(callback /*, initialValue*/) { 'use strict'; if (null === this || 'undefined' === typeof this) { throw new TypeError('Array.prototype.reduce called on null or undefined'); } if ('function' !== typeof callback) { throw new TypeError(callback + ' is not a function'); } var t = Object(this), len = t.length >>> 0, k = len - 1, value; if (arguments.length >= 2) { value = arguments[1]; } else { while (k >= 0 && !(k in t)) { k--; } if (k < 0) { throw new TypeError('Reduce of empty array with no initial value'); } value = t[k--]; } for (; k >= 0; k--) { if (k in t) { value = callback(value, t[k], k, t); } } return value; }; }
規範
Specification | Status | Comment |
---|---|---|
ECMAScript 5.1 (ECMA-262) The definition of 'Array.prototype.reduceRight' in that specification. |
Standard | Initial definition. Implemented in JavaScript 1.8. |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.prototype.reduceRight' in that specification. |
Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'Array.prototype.reduceRight' in that specification. |
Draft |
瀏覽器相容性
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | Yes | Yes | 3 | 9 | 10.5 | 4 |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | 4 | Yes | Yes | Yes |