First time working with a shopping cart component, I have my code structured like this..
let cartCount = document.getElementById("counter");
let isItemSelected = false;
let itemCount = 0;
let shoppingCart = [];
let selectedSize = "";
let displaySelectedSize = document.getElementById("selected");
let displayCart = document.getElementById("cart");
selectItem = () => {
isItemSelected = true;
selectedSize = event.srcElement.id;
displaySelectedSize.innerHTML = `${selectedSize}`;
}
addItem = () => {
if (isItemSelected === true) {
const shopItem = {
name: "Classic Tee",
price: 75,
size: `${selectedSize}`,
quantity: 0
}
itemCount+= 1;
cartCount.innerHTML = `( ${itemCount} )`;
shopItem.quantity++
shoppingCart.push(shopItem);
console.log(shoppingCart);
return itemSuccess();
} else {
return itemError();
}
}
What I basically need is for the shopItem.quantity to increase if I have multiple of the selected size.
At the moment I get this..
// Output
0: {name: "Classic Tee", price: 75, size: "small", quantity: 1}
1: {name: "Classic Tee", price: 75, size: "small", quantity: 1}
// Desired Output
0: {name: "Classic Tee", price: 75, size: "small", quantity: 2}
I can see why my output is like that, it's creating a new object each time my addItem function is fired, but I would like to mutate my shopItem.quantity if there are duplicates...
How would I do that?