I have a shopping cart and I only want the products to be set if they don’t already exist in localStorage. If the product is already in localStorage, then no new product should be added. Then I need to update the quantity if the item is already set. The code that I have so far sets the product even if the product already exists and it doesn’t update the quantity.
public onSubmit(id, thumbnail, quantity, product_name, product_price){
var data = {
id,
thumbnail,
quantity,
product_name,
product_price
};
var retrieverObject = localStorage.getItem('items');
var retrieveObject = JSON.parse(retrieverObject);
retrieveObject.forEach(el => {
if (el.id == id){
this.quantity += quantity;
} else {
this.items.push(data);
localStorage.setItem(this.storageKey, JSON.stringify(this.items));
}
});
}