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;
}
}
添加回答
舉報