array.reduce() 方法用于通過對每個元素執行一些任務來將整個數組縮減為單個值。例如,當我們想要得到數組所有元素的總和時,我們需要將整個數組簡化為單個值,該值就是數組所有元素的最終總和。
array.reduce() 方法跟蹤前一個元素的結果值。之后,它使用我們從前一個元素獲得的結果值對下一個元素執行任務。數組的第一個元素考慮作為結果值的參數傳遞的初始值。在本教程中,我們將學習使用 JavaScript 的 Array.prototype.reduce() 方法。
語法
用戶可以按照以下語法使用 array.reduce() 方法。
array.reduce((previousResult, element, index, array) => { // perform a task }, startingValue);
登錄后復制
我們在上面的語法中將箭頭函數作為第一個參數值傳遞。箭頭函數用作內聯回調函數。
array.reduce(callback, startingValue);
登錄后復制
在上面的語法中,回調是一個回調函數。
參數
previousResult – 這是我們對前一個數組元素執行一些操作得到的結果值。
element – 它是數組中索引位置的元素。
Index – 它是數組元素的當前索引。
Array – 它本身就是一個在回調函數中使用的數組。
startingValue – 這是初始化 previousResult 變量的初始值。
callback – 這是一個調用數組中每個元素的函數。
返回值
array.reduce() 方法在對所有數組元素執行某些任務后返回最終結果值。
示例 1
在下面的示例中,我們創建了數字數組并用一些數值對其進行了初始化。之后,我們使用 array.reduce() 方法來求所有數字的乘積。
此外,我們還使用內聯回調函數作為reduce()方法的第一個參數。在回調函數中,我們將 previousResult 變量的值與元素相乘并返回它。
<html> <body> <h2>Using the <i>array.reduce()</i> method to find a factorial of a number in JavaScript.</h2> <div id = "output"> </div> <script> let output = document.getElementById('output'); let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let factorial = numbers.reduce((previousResult, element) => { return previousResult = element * previousResult; }, 1) output.innerHTML += "The factorial of 10 is " + factorial; </script> </body> </html>
登錄后復制
示例 2
在下面的示例中,我們使用 array.reduce() 方法將所有數組字符串連接到一個字符串中。我們使用“+”運算符將當前字符串元素與前一個結果合并到回調函數中。
<html> <body> <h2>Using the <i>array.reduce()</i> method to merge all strings of the array in JavaScript.</h2> <div id="output"> </div> <script> let output = document.getElementById('output'); let strings = ["Welcome", "To", "the", "TutorialsPoint", "blogs", "!"]; function callback(previousResult, stringElement) { return previousResult + " " + stringElement; } let message = strings.reduce(callback, ""); output.innerHTML += "The Message created from the array of the string values is " + message; </script> </body> </html>
登錄后復制
示例 3
在下面的示例中,我們正在查找元素索引值的總和。用戶可以在回調函數中看到我們如何使用數組索引。
<html> <body> <h2>Using the <i>array.reduce()</i> method.</h2> <div id = "output"> </div> <script> let output = document.getElementById('output'); let numersArray = [20, 30, 40, 50, 60, 70, 80, 90, 100]; let finalValue = numersArray.reduce((previousResult, element, index, array) => { return previousResult + element - index; }, 0); output.innerHTML += "The resultant value after performing operations on array element is " + finalValue; </script> </body> </html>
登錄后復制
示例 4
在此示例中,我們創建了一個對象數組。該數組的每個對象都包含 emp_id、emp_name 和工資。我們使用了reduce()方法來獲取所有員工的工資總額。在reduce()方法的回調函數中,我們訪問每個對象并將其salary屬性的值添加到total變量中。最后返回所有員工的工資總額。
<html> <body> <h2>Using the <i> array.reduce() </i> method.</h2> <div id = "output"> </div> <script> let output = document.getElementById('output'); let arrayOfObjects = [ { emp_id: "10", emp_name: "Shubham", salary: 10000 }, { emp_id: "20", emp_name: "Akshay", salary: 20000 }, { emp_id: "dfd0", emp_name: "John", salary: 20000 }, { emp_id: "10", emp_name: "Mukund", salary: 50000 }, { emp_id: "10", emp_name: "salman", salary: 5000 } ] let totalSalary = arrayOfObjects.reduce((total, object, index, array) => { return total + object.salary; }, 0); output.innerHTML += "The total salary of all employees is " + totalSalary; </script> </body> </html>
登錄后復制
用戶學會了使用 Array.prototype.reduce() 方法將整個數組轉換為單個數組值。我們已經通過不同的例子看到了reduce()方法的用例。另外,我們可以使用 array.reduce() 方法從數組中查找最小值和最大值。
當我們以空數組作為引用來調用 array.reduce() 方法時,它會返回一個錯誤。
以上就是如何在 JavaScript 中使用 Array.prototype.reduce() 方法?的詳細內容,更多請關注www.92cms.cn其它相關文章!