JavaScript 的 reduce() 函式

JavaScript 中 reduce() 函式的詳細說明 reduce() 是陣列中另一個重要的方法。 reduce() 在陣列的每個項目上執行回呼函式,並允許逐步計算結果。如果指定了 initialValue,那麼在第一次迭代中,accumulator 將等於該值。 a.reduce((accumulator, currentValue, currentIndex, array) => { //... }, initialValue) 範例: [1, 2, 3, 4].reduce((accumulator, currentValue, currentIndex, array) => { return accumulator * currentValue }, 1) // 迭代 1:1 * 1 => 回傳 1 // 迭代 2:1 * 2 => 回傳 2 // 迭代 3:2 * 3 => 回傳 6 // 迭代 4:6 * 4 => 回傳 24 // 回傳值為 24