2 回答
TA贡献1843条经验 获得超7个赞
我为你创造了一个例子。您可以使用以下代码获取产品列表的小计
listproducts=[{totalcost:50},{totalcost:30},{totalcost:150}];
function FnSubtotal(products){
subtotal=0;
if(products.length == 0){
return 0;
}
for(i = 0; i<products.length; i++){
subtotal += products[i].totalcost;
}
return subtotal;
};
console.log(FnSubtotal(listproducts))
TA贡献1884条经验 获得超4个赞
请尝试以下代码来添加所有产品总成本
function subtotal(){
var total=0;
if(products.length == 1){
total = totalcost;
return total;
}
else if(products.length > 1){
for(var i = 0; i<products.length; i++){
total += products[i].totalcost;
}
return total;
}
}
添加回答
举报
