JavaScript數組的高級用法-reduce和reduceRight詳解

reduce 方法(升序)

語法:   

    array1.reduce(callbackfn[, initialValue])

參數

定義

array1

必需。一個數組對象。

callbackfn

必需。一個接受最多四個參數的函數。對於數組中的每個元素,reduce 方法都會調用 callbackfn 函數一次。

initialValue

可選。如果指定 initialValue,則它將用作初始值來啓動累積。第一次調用 callbackfn 函數會將此值作爲參數而非數組值提供

返回值:

        通過最後一次調用回調函數獲得的累積結果。

異常:

        當滿足下列任一條件時,將引發 TypeError 異常:

  • callbackfn 參數不是函數對象。

  • 數組不包含元素,且未提供 initialValue

回調函數語法:

    function callbackfn(previousValue, currentValue, currentIndex, array1)

    可使用最多四個參數來聲明回調函數。

    下表列出了回調函數參數。

回調參數

定義

previousValue

通過上一次調用回調函數獲得的值。如果向 reduce 方法提供 initialValue,則在首次調用函數時,previousValue 爲 initialValue

currentValue

當前數組元素的值。

currentIndex

當前數組元素的數字索引。

array1

包含該元素的數組對象。

第一次調用回調函數

在第一次調用回調函數時,作爲參數提供的值取決於 reduce 方法是否具有 initialValue 參數。

如果向 reduce 方法提供 initialValue

  • previousValue 參數爲 initialValue

  • currentValue 參數是數組中的第一個元素的值。

如果未提供 initialValue

  • previousValue 參數是數組中的第一個元素的值。

  • currentValue 參數是數組中的第二個元素的值。

修改數組對象

數組對象可由回調函數修改。

下表描述了在 reduce 方法啓動後修改數組對象所獲得的結果。


reduce 方法啓動後的條件

元素是否傳遞給回調函數

在數組的原始長度之外添加元素。

否。

添加元素以填充數組中缺少的元素。

是,如果該索引尚未傳遞給回調函數。

元素被更改。

是,如果該元素尚未傳遞給回調函數。

從數組中刪除元素。

否,除非該元素已傳遞給回調函數。


實例:

1.下面的示例將數組值連接成字符串,各個值用“::”分隔開由於未向 reduce 方法提供初始值,第一次調用回調函數時會將“abc”作爲 previousValue 參數並將“def”作爲 currentValue 參數。

  1. function appendCurrent (previousValue, currentValue) {
  2.    return previousValue + "::" + currentValue;
  3.    }
  4. var elements = ["abc", "def", 123, 456];
  5. var result = elements.reduce(appendCurrent);
  6. document.write(result);
  7. // Output:
  8. //  abc::def::123::456

2.下面的示例向數組添加舍入後的值。使用初始值 0 調用 reduce 方法。

  1. function addRounded (previousValue, currentValue) {
  2.    return previousValue + Math.round(currentValue);
  3.    }
  4. var numbers = [10.9, 15.4, 0.5];
  5. var result = numbers.reduce(addRounded, 0);
  6. document.write (result);
  7. // Output: 27

3.下面的示例向數組中添加值。 currentIndex 和 array1 參數用於回調函數

  1. function addDigitValue(previousValue, currentDigit, currentIndex, array) {
  2.    var exponent = (array.length - 1) - currentIndex;
  3.    var digitValue = currentDigit * Math.pow(10, exponent);
  4.    return previousValue + digitValue;
  5.    }
  6. var digits = [4, 1, 2, 5];
  7. var result = digits.reduce(addDigitValue, 0);
  8. document.write (result);
  9. // Output: 4125

此題分析:

    首先賦予了初始值0,那麼currentDigit就是從4開始的,調用方法四次,這樣可以把四次方法調用的參數都寫出來:(0,4,0,array)、(4,1,1,array)、(1,2,2,array)、(2,5,3,array),再一次進行計算,由於初始值是0,所有隻需要計算出每個方法的返回值最後相加即可。array.length始終爲4,則四次計算的值分別爲4000+100+20+5=4125


reduceRight 方法(降序)


 reduceRight的語法以及回調函數的規則和reduce方法是一樣的,區別就是在與reduce是升序,即角標從0開始,而reduceRight是降序,即角標從arr.length-1開始。如果有初始值,則從最後一個數開始計算,如果沒有初始值,則previousValue參數是數組中最後一個元素的值,currentValue是數組中倒數第二個元素的值。

示例:

1.下面的示例獲取數組中值爲 1 到 10 之間的元素。提供給 reduceRight 方法的初始值是一個空數組。

  1. function Process2(previousArray, currentValue) {
  2.    var nextArray;
  3.    if (currentValue >= 1 && currentValue <= 10)
  4.        nextArray = previousArray.concat(currentValue);
  5.    else
  6.        nextArray = previousArray;
  7.    return nextArray;
  8. }
  9. var numbers = [20, 1, -5, 6, 50, 3];
  10. var emptyArray = new Array();
  11. var resultArray = numbers.reduceRight(Process2, emptyArray);
  12. document.write("result array=" + resultArray);
  13. // Output:
  14. //  result array=3,6,1
2.reduceRight 方法可應用於字符串。下面的示例演示如何使用此方法反轉字符串中的字符。
  1. function AppendToArray(previousValue, currentValue) {
  2.    return previousValue + currentValue;
  3. }
  4. var word = "retupmoc";
  5. var result = [].reduceRight.call(word, AppendToArray, "the ");
  6. // var result = Array.prototype.reduceRight.call(word, AppendToArray, "the ");
  7. document.write(result);
  8. // Output:
  9. // the computer
這裏可以直接使用空數組調用reduceRight方法,並且使用call方法將參數引入。也可以是直接使用原型鏈的方式進行調用,即Array.prototype.reduceRight.call(word, AppendToArray, "the ");


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章