js 遍历数组对象求和

这个通常是求多个商品的总价遇到的情形:

[
    0: {id: 1, name: "服务费", price: "1.00"}
    1: {id: 2, name: "邮寄费", price: "1.00"}
]

求和总价:

let totalPrice = 0
data.reduce((totalPrice, item) => totalPrice + item.price, 0)

猜你喜欢

转载自www.cnblogs.com/pjl43/p/11647695.html