<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                對數組中的所有元素調用指定的回調函數。該回調函數的返回值為累積結果,并且此返回值在下一次調用該回調函數時作為參數提供。 ## 語法 ~~~ array1.reduce(callbackfn[, initialValue]) ~~~ ## 參數 |參數|定義| |--|--| |array1|必需。一個數組對象。| |callbackfn|必需。一個接受最多四個參數的函數。對于數組中的每個元素,reduce 方法都會調用 callbackfn 函數一次。| |initialValue|可選。如果指定 initialValue,則它將用作初始值來啟動累積。第一次調用 callbackfn 函數會將此值作為參數而非數組值提供。| ## 返回值 通過最后一次調用回調函數獲得的累積結果。 ## 異常 當滿足下列任一條件時,將引發 TypeError 異常: - callbackfn 參數不是函數對象。 - 數組不包含元素,且未提供 initialValue。 ~~~ Exception Condition ~~~ ## 備注 如果提供了 initialValue,則 reduce 方法會對數組中的每個元素調用一次 callbackfn 函數(按升序索引順序)。如果未提供 initialValue,則 reduce 方法會對從第二個元素開始的每個元素調用 callbackfn 函數。 回調函數的返回值在下一次調用回調函數時作為 previousValue 參數提供。最后一次調用回調函數獲得的返回值為 reduce 方法的返回值。 不為數組中缺少的元素調用該回調函數。 > 注意:reduceRight 方法 (Array) (JavaScript)按降序索引順序處理元素。 ### 回調函數語法 回調函數的語法如下所示: ~~~ 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 方法啟動后的條件|元素是否傳遞給回調函數| |--|--| |在數組的原始長度之外添加元素。|否。| |添加元素以填充數組中缺少的元素。|是,如果該索引尚未傳遞給回調函數。| |元素被更改。|是,如果該元素尚未傳遞給回調函數。| |從數組中刪除元素。|否,除非該元素已傳遞給回調函數。| 下面的示例將數組值連接成字符串,各個值用“::”分隔開。由于未向 reduce 方法提供初始值,第一次調用回調函數時會將“abc”作為 previousValue 參數并將“def”作為 currentValue 參數。 ~~~ // Define the callback function. function appendCurrent (previousValue, currentValue) { return previousValue + "::" + currentValue; } // Create an array. var elements = ["abc", "def", 123, 456]; // Call the reduce method, which calls the callback function // for each array element. var result = elements.reduce(appendCurrent); document.write(result); // Output: // abc::def::123::456 ~~~ 下面的示例向數組添加舍入后的值。使用初始值 0 調用 reduce 方法。 ~~~ // Define the callback function. function addRounded (previousValue, currentValue) { return previousValue + Math.round(currentValue); } // Create an array. var numbers = [10.9, 15.4, 0.5]; // Call the reduce method, starting with an initial value of 0. var result = numbers.reduce(addRounded, 0); document.write (result); // Output: 27 ~~~ 下面的示例向數組中添加值。 currentIndex 和 array1 參數用于回調函數。 ~~~ function addDigitValue(previousValue, currentDigit, currentIndex, array) { var exponent = (array.length - 1) - currentIndex; var digitValue = currentDigit * Math.pow(10, exponent); return previousValue + digitValue; } var digits = [4, 1, 2, 5]; // Determine an integer that is computed from values in the array. var result = digits.reduce(addDigitValue, 0); document.write (result); // Output: 4125 ~~~ 下面的示例獲取一個數組,該數組僅包含另一個數組中的介于 1 和 10 之間值。提供給 reduce 方法的初始值是一個空數組。 ~~~ function Process(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 reduce method, // the returned array is previousArray on the next call. // If this is the last call by the reduce method, the // returned array is the return value of the reduce method. return nextArray; } // Create an array. var numbers = [20, 1, -5, 6, 50, 3]; // Call the reduce method, starting with an initial empty array. var emptyArray = new Array(); var resultArray = numbers.reduce(Process, emptyArray); document.write("result array=" + resultArray); // Output: // result array=1,6,3 ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看