Здесь у меня есть массив товаров который добавить товары в корзину. Как я могу удалить продукт из массива после того, как я добавил его в корзина от ID?Как связать массив корзины с массивом продуктов?
//Product class
function Product(options) {
this.id = options.id;
this.name = options.name;
this.cost = options.cost;
this.quantity = options.quantity;
this.shortDescription = options.shortDescription;
this.fullDescription = options.fullDescription;
}
//Basket class
function Cart() {
this.items = [];
}
//Add product in basket
Cart.prototype.addItem = function(options) {
//If we find the same element in array by name, just +1 to quantity in basket
options.quantity = 1;
for (var i in this.items) {
if (this.items[i].id === options.id) {
this.items[i].quantity += options.quantity;
return;
}
}
var item = new Product(options);
this.items.push(item);
};
var arrayOfProducts = [
{id:1, name:'book', cost:5.45, quantity:5, shortDescription: 'Short description about book', fullDescription: 'Full description about book'},
{id:2, name:'pan', cost:7.31, quantity:2, shortDescription: 'Short description about pan', fullDescription: 'Full description about pan'},
{id:3, name:'cup', cost:9.37, quantity:4, shortDescription: 'Short description about cup', fullDescription: 'Full description about cup'},
]
cart.addItem(arrayOfProducts [0]);
cart.addItem(arrayOfProducts [0]);
cart.addItem(arrayOfProducts [1]);
Мы увидим это в массиве корзину
id:1, name:book, cost:5.45, quantity:2, short description: Short description about book, full description: Full description about book
,id:2, name:pan, cost:7.31, quantity:1, short description: Short description about pan, full description: Full description about pan