gpt4 book ai didi

javascript - 计算总价

转载 作者:行者123 更新时间:2023-11-30 16:13:21 25 4
gpt4 key购买 nike

我有一系列产品,我想计算所有产品的总价,但我不知道该怎么做。有什么建议吗?

 CountTotal: function() {
var total;
var totalsum;
this.state.data.forEach(function(item, index) {
total += item.price + item.quantity;
totalsum += total + index.length;
});
console.log(totalsum);
},

最佳答案

 CountTotal: function() {
var total;
this.state.data.forEach(function(item, index) {
total += item.price * item.quantity;
});
console.log(total);
},

或者用Array#reduce

 CountTotal: function() {
var total = this.state.data.reduce(function(res,item) {
return res + (item.price * item.quantity);
}, 0);
console.log(total);
},

关于javascript - 计算总价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35893492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com