按降序順序對數組中的所有元素調用指定的回調函數。該回調函數的返回值為累積結果,并且此返回值在下一次調用該回調函數時作為參數提供。
## 語法
~~~
array1.reduceRight(callbackfn[, initialValue])
~~~
## 參數
|參數|定義|
|--|--|
|array1|必需。一個數組對象。|
|callbackfn|必需。一個接受最多四個參數的函數。對于數組中的每個元素,reduceRight 方法都會調用 callbackfn 函數一次。|
|initialValue|可選。如果指定 initialValue,則它將用作初始值來啟動累積。第一次調用 callbackfn 函數會將此值作為參數而非數組值提供。|
## 返回值
包含通過最后一次調用回調函數獲得的累積結果的對象。
## 異常
當滿足下列任一條件時,將引發 TypeError 異常:
- callbackfn 參數不是函數對象。
- 數組不包含元素,且未提供 initialValue。
~~~
Exception Condition
~~~
## 備注
如果提供了 initialValue,則 reduceRight 方法會按降序索引順序對數組中的每個元素調用一次 callbackfn 函數。如果未提供 initialValue,則 reduceRight 方法會按降序索引順序對每個元素(從倒數第二個元素開始)調用 callbackfn 函數。
回調函數的返回值在下一次調用回調函數時作為 previousValue 參數提供。最后一次調用回調函數獲得的返回值為 reduceRight 方法的返回值。
不為數組中缺少的元素調用該回調函數。
若要按升序索引順序累積結果,請使用 reduce 方法 (Array) (JavaScript)。
### 回調函數語法
回調函數的語法如下所示:
~~~
function callbackfn(previousValue, currentValue, currentIndex, array1)
~~~
可使用最多四個參數來聲明回調函數。
下表列出了回調函數參數。
|回調參數|定義|
|--|--|
|previousValue|通過上一次調用回調函數獲得的值。如果向 reduceRight 方法提供 initialValue,則在首次調用函數時,previousValue 為 initialValue。|
|currentValue|當前數組元素的值。|
|currentIndex|當前數組元素的數字索引。|
|array1|包含該元素的數組對象。|
### 第一次調用回調函數
在第一次調用回調函數時,作為參數提供的值取決于 reduceRight 方法是否具有 initialValue 參數。
如果向 reduceRight 方法提供 initialValue:
- previousValue 參數為 initialValue。
- currentValue 參數是數組中的最后一個元素的值。
如果未提供 initialValue:
- previousValue 參數是數組中的最后一個元素的值。
- currentValue 參數是數組中的倒數第二個元素的值。
### 修改數組對象
數組對象可由回調函數修改。
下表描述了在 reduceRight 方法啟動后修改數組對象所獲得的結果。
|reduceRight方法啟動后的條件|元素是否傳遞給回調函數|
|--|--|
|在數組的原始長度之外添加元素。|否。|
|添加元素以填充數組中缺少的元素。|是,如果該索引尚未傳遞給回調函數。|
|元素被更改。|是,如果該元素尚未傳遞給回調函數。|
|從數組中刪除元素。|否,除非該元素已傳遞給回調函數。|
下面的示例將數組值連接成字符串,各個值用“::”分隔開。由于未向 reduceRight 方法提供初始值,第一次調用回調函數時會將 456 作為 previousValue 參數并將 123 作為 currentValue 參數。
~~~
// Define the callback function.
function appendCurrent (previousValue, currentValue) {
return previousValue + "::" + currentValue;
}
// Create an array.
var elements = ["abc", "def", 123, 456];
// Call the reduceRight method, which calls the callback function
// for each array element, in descending index order.
var result = elements.reduceRight(appendCurrent);
document.write(result);
// Output:
// 456::123::def::abc
~~~
下面的示例查找數組元素的平方和。使用初始值 0 調用 reduceRight 方法。
~~~
// Define the callback function.
function Process(previousValue, currentValue, index, array) {
// Add the previous value to the current value squared.
var nextValue = previousValue + (currentValue * currentValue);
// If this is not the last call by the reduceRight method,
// the return value is previousValue on the next call.
// If this is the last call by the reduceRight method, the
// return value is the return value of the reduceRight method.
return nextValue;
}
// Create an array.
var numbers = [3, 4, 5];
// Call the reduceRight method with an initial value of 0.
var sumOfSquares = numbers.reduceRight(Process, 0);
document.write("sum of squares=" + sumOfSquares);
// Output:
// sum of squares=50
~~~
下面的示例獲取數組中值為 1 到 10 之間的元素。提供給 reduceRight 方法的初始值是一個空數組。
~~~
function Process2(previousArray, currentValue) {
// If currentValue is between 1 and 10,
// append currentValue to the array.
var nextArray;
if (currentValue >= 1 && currentValue <= 10)
nextArray = previousArray.concat(currentValue);
else
nextArray = previousArray;
// If this is not the last call by the reduceRight method,
// the returned array is previousArray on the next call.
// If this is the last call by the reduceRight method, the
// returned array is the return value of the reduceRight method.
return nextArray;
}
// Create an array.
var numbers = [20, 1, -5, 6, 50, 3];
// Call the reduceRight method, starting with an empty array.
var emptyArray = new Array();
var resultArray = numbers.reduceRight(Process2, emptyArray);
document.write("result array=" + resultArray);
// Output:
// result array=3,6,1
~~~
reduceRight 方法可應用于字符串。下面的示例演示如何使用此方法反轉字符串中的字符。
~~~
// Define the callback function.
function AppendToArray(previousValue, currentValue) {
return previousValue + currentValue;
}
var word = "retupmoc";
// Create a string that reverses the characters of another string.
// The commented-out statement shows an alternative syntax.
var result = [].reduceRight.call(word, AppendToArray, "the ");
// var result = Array.prototype.reduceRight.call(word, AppendToArray, "the ");
document.write(result);
// Output:
// the computer
~~~
- ActiveXObject對象
- Array對象
- constructor屬性
- length屬性
- prototype屬性
- Array.from函數
- Array.isArray函數
- Array.of函數
- concat方法
- entries方法
- every方法
- fill方法
- filter方法
- findIndex方法
- forEach方法
- indexOf方法
- join方法
- keys方法
- lastIndexOf方法
- map方法
- pop方法
- push方法
- reduce方法
- reduceRight方法
- reverse方法
- shift方法
- slice方法
- some方法
- sort方法
- splice方法
- toString方法
- unshift方法
- valueOf方法
- values方法
- ArrayBuffer對象
- byteLength屬性
- ArrayBuffer.isView函數
- slice方法
- arguments對象
- 0...n 屬性(參數)
- callee 屬性(參數)
- length 屬性 (arguments)
- Boolean對象
- constructor 屬性(布爾值)
- prototype 屬性(布爾值)
- toString 方法 (Boolean)
- valueOf 方法 (Boolean)
- DataView對象
- buffer屬性 (DataView)
- byteLength屬性(DataView)
- byteOffset屬性(DataView)
- getInt8方法(DataView)
- getUint8方法(DataView)
- getInt16方法(DataView)
- getUint16方法(DataView)
- getInt32方法(DataView)
- getUint32方法(DataView)
- getFloat32方法(DataView)
- getFloat64方法(DataView)
- setInt8方法(DataView)
- setUint8方法(DataView)
- setInt16方法(DataView)
- setUint16方法(DataView)
- setInt32方法(DataView)
- setUint32方法(DataView)
- setFloat32方法(DataView)
- setFloat64方法(DataView)
- Date對象
- Debug對象
- Enumerator對象
- Error對象
- Float32Array對象
- Float64Array對象
- Function對象
- Global對象
- Int8Array對象
- Int16Array對象
- Int32Array對象
- Intl.Collator對象
- Intl.DateTimeFormat對象
- Intl.NumberFormat對象
- JSON對象
- Map對象
- Math對象
- Number對象
- Object對象
- Promise對象
- 代理對象
- Reflect對象
- RegExp對象
- 正則表達式對象
- Set對象
- String對象
- 符號對象
- Uint8Array對象
- Uint8ClampedArray對象
- Uint16Array對象
- Uint32Array對象
- VBArray對象
- WeakMap對象
- WeakSet對象
- WinRTError對象