I have a shopping cart that is a nested list. I want to check if a certain item is already in the list before adding it again. If it is already in the list I just want to update the quantity. What am I doing wrong here, and how can I implement localstorage here (hypothetically)
Here is the function:
function addToCart(e) {
var updatedCart = [];
if (cart.includes(e.target.innerText)) {
console.log("Duplicate found");
for (var i = 0; i < cart.length; i++) {
if (cart[i][0] == e.target.innerText) {
cart[i][1]++;
updatedCart.push(cart[i]);
i++;
} else {
i++;
}
}
} else {
setCart((cart) => [...cart, [e.target.innerText, 1]]);
}
}