【发布时间】:2022-01-31 08:28:23
【问题描述】:
我有一个装有苹果和橙色物体的购物车。我希望能够添加两次苹果,但我不确定如何将“fruit1”对象定位两次,以便在购物车中显示 2 个苹果。我基本上想用“2个苹果”更新购物车数组,价格“价值”翻倍,1个橙色)
module.exports = {
shoppingCart: function () {
//create empty cart array
theCart = [];
// all fruits
let fruitProducts = [
{
fruit1: 'Apple',
price: 4.95,
},
{
fruit2: 'Orange',
price: 3.99,
},
];
//push the objects into the empty array using the apply method -- add items to the cart
Array.prototype.push.apply(theCart, fruitProducts);
//remove items from the cart by calling this function
function removeAllItems() {
if ((theCart.length = !0)) {
theCart = [];
}
}
//removeAllItems();
console.log(theCart);
},
};
【问题讨论】:
-
建议: 1. 在每个购物车条目中添加数量和小计字段。 2. 不要使用fruit1 和fruit2,只使用fruit - 或者更好的是item。
-
感谢您的帮助,约翰。仍在努力,但绝对有帮助
标签: javascript node.js arrays object