reduce,+=,_.sumBy累加求和

reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值
 

 

handleSelectionChange(val) {
      this.selectList = val
//方法一
      // this.sum2 = 0
      // arr.forEach(item => {
      //   this.sum2 += item.dblamount
      // })
    },
computed: {
//方法二
    sum() {
      return this.selectList.reduce((all, val) => { return all + val.dblamount }, 0).toLocaleString()
    }
//方法三
    // sum() {
    //   return this._.sumBy(this.selectList, o => Number(o.dblamount))
    // }
  }
 
 
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
 
参数
描述
function(total,currentValue, index,arr)
必需。用于执行每个数组元素的函数。
函数参数:
参数
描述
total
必需。
初始值
, 或者计算结束后的返回值。
currentValue
必需。当前元素
currentIndex
可选。当前元素的索引
arr
可选。当前元素所属的数组对象。
initialValue
可选。传递给函数的初始值
 
原文地址:https://www.cnblogs.com/hellofangfang/p/14434643.html