hi, 欢迎访问我的博客

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)

 

posted @ 2019-10-10 14:21  打静爵  阅读(18121)  评论(1编辑  收藏  举报