over 3 years ago
let state = {items: [
    { name: 'apple', value: 1 },
    { name: 'orange', value: 2 },
    { name: 'tomato', value: 3 },
]}
// 第一個參數 callback => function(previousValue, currentValue, currentIndex, array) { return... }
// 第二個參數 起始值 
let sum = items.reduce((acc, item) => acc + item.value, 0);
console.log("sum = " + sum); // sum = 6
// 0 為起始值 一開始 acc = 0

sum = items.reduce((acc, item) => acc + item.value, 10);
console.log("sum = " + sum); // sum = 16
// 10 為起始值 一開始 acc = 10
← 開發階段時 Webpack 與 Server 端的整合配置 Array.prototype.filter() 過濾器 →
 
comments powered by Disqus