计算总金额的方法
sumMoney(table){
const sumList = [] 声明一个空数组,把需要计算的值放到这个空数组里
table.forEach(p => { 从table里面拿出需要计算的金额放到sumlist数组里面
if(p.chargeIndicator != 2){
sumList.push(p.costs)
}
})
通过reduce函数对总金额进行计算
this.totalPrice = sumList.reduce((prev, next) => {
return prev + next;
}, 0);
对总金额保留两位小数处理 this.totalPrice便是我声明的总金额字段
this.totalPrice=this.totalPrice.toFixed(2)
},
本文介绍了一种使用JavaScript遍历表格数据并计算总金额的方法。通过将符合条件的数据项存入数组,再利用reduce函数进行累加求和,最终得到总金额,并将其保留两位小数。
2763

被折叠的 条评论
为什么被折叠?



