<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.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 ~~~
                  <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>

                              哎呀哎呀视频在线观看