reduce 计算商品列表中某一列(单品售价)的累加和( 整单售价)
orderCommoditys 商品列表数组
let allList = (this.orderCommoditys || []).reduce(function (a, b) {
return {
// 数量
quantityAll: NP.plus(a.quantityAll, Number(b.quantity) || 0),
// 基准价 总基准价自营:商品明细的basePrice*quantity
basePriceAll: NP.plus(a.basePriceAll, NP.times(Number(b.quantity) || 0 , Number(b.basePrice) || 0))
}
}, {basePriceAll: 0, openPriceAll: 0, quantityAll: 0})å
this.quantityAll = allList.quantityAll;
this.basePriceAll = allList.basePriceAll;
{basePriceAll: 0, openPriceAll: 0, quantityAll: 0}
NP传参数至少两个值, 如果数组中无返回值的时候会报错,所以给初始值默认传零
解决js 计算精度问题number-precision
- 安装
npm install number-precision --save
- 方法
import NP from 'number-precision&