將0當做reduce回調函數中的初始值,然后依次累加
// 數組求和
var arr = [1, 2, 3, 4, 5]
var total = arr.reduce(function (prev, next) {
return prev + next
}, 0)
console.log(total)
將0當做reduce回調函數中的初始值,然后依次累加
// 數組求和
var arr = [1, 2, 3, 4, 5]
var total = arr.reduce(function (prev, next) {
return prev + next
}, 0)
console.log(total)
本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。