javascript
reduce 方法 (Array) (JavaScript)
概念:
對數組中的所有元素調用指定的回調函數。該回調函數的返回值為累積結果,并且此返回值在下一次調用該回調函數時作為參數提供。
語法:
array1.reduce(callbackfn[, initialValue])
參數:
1. array1 => 必需。一個數組對象。
2. callbackfn => 必需。一個接受最多四個參數的函數。對于數組中的每個元素,reduce 方法都會調用 callbackfn 函數一次
3. initialValue => 可選。如果指定 initialValue,則它將用作初始值來啟動累積。第一次調用 callbackfn 函數會將此值作為參數而非數組值提供。
返回值:
通過最后一次調用回調函數獲得的累積結果。
非常重要的的一段話
如果提供了 initialValue,則 reduce 方法會對數組中的每個元素調用一次 callbackfn 函數(按升序索引順序)。如果未提供 initialValue,則 reduce 方法會對從第二個元素開始的每個元素調用 callbackfn 函數。
回調函數的返回值在下一次調用回調函數時作為 previousValue 參數提供。最后一次調用回調函數獲得的返回值為 reduce 方法的返回值。
不為數組中缺少的元素調用該回調函數。
回調函數
1. function callbackfn(previousValue, currentValue, currentIndex, array1) ?? => ?? 可使用最多四個參數來聲明回調函數。
2. 參數:
previousValue =>?通過上一次調用回調函數獲得的值。如果向 reduce 方法提供 initialValue,則在首次調用函數時,previousValue 為 initialValue。
currentValue =>?當前數組元素的值
currentIndex =>?當前數組元素的數字索引
array1 =>?包含該元素的數組對象
第一次調用函數時
在第一次調用回調函數時,作為參數提供的值取決于 reduce 方法是否具有 initialValue 參數。
如果向 reduce 方法提供 initialValue:
previousValue 參數為 initialValue。
currentValue 參數是數組中的第一個元素的值。
如果未提供 initialValue:
previousValue 參數是數組中的第一個元素的值。
currentValue 參數是數組中的第二個元素的值。
說了這么多,來個些實例
1. 下面的示例將數組值連接成字符串,各個值用“::”分隔開。由于未向 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::4562.下面的示例向數組添加舍入后的值。使用初始值 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: 273.下面的示例向數組中添加值。 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: 41254. 下面的示例獲取一個數組,該數組僅包含另一個數組中的介于 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);elsenextArray = 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備注:在以下文檔模式中不受支持:Quirks、Internet Explorer 6 標準模式、Internet Explorer 7 標準模式、Internet Explorer 8 標準模式。
原文參考鏈接:https://msdn.microsoft.com/library/ff679975(v=vs.94).aspx
轉載于:https://www.cnblogs.com/huhulove/p/5711958.html
總結
以上是生活随笔為你收集整理的reduce 方法 (Array) (JavaScript)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU 5514 Frogs (容斥原理
- 下一篇: QT Creator常用快捷键