Solved Now - sessionStorage->array->make New Array without item -> back to sessionStorage
This is driving me nuts, and yes have looked all day for a solution.
I'm trying to remove item from the sessionStorage. But just one item. I can find the item, just can't remove it. Any help would be great, Thanks
function removeCartItem(key) {
if (sessionStorage.getItem('shopping-cart')) {
var IsItemThere = false;
var shoppingCart = JSON.parse(sessionStorage.getItem('shopping-cart'));
shoppingCart.forEach(function(item) {
var cartItem = JSON.parse(item);
if(cartItem.key === key){
IsItemThere = true;
}
});
if(IsItemThere == true){
var cartArray = new Array();
shoppingCart.forEach(function(item) {
var cartItem = JSON.parse(item);
if(cartItem.key != key){
cartItem = {
key: cartItem.key,
id: cartItem.id,
productName: cartItem.productName,
price: cartItem.price,
quantity: cartItem.quantity
};
var cartItemJSON = JSON.stringify(cartItem);
cartArray.push(cartItemJSON);
}
});
}
sessionStorage.removeItem('shopping-cart');
var cartJSON = JSON.stringify(cartArray);
sessionStorage.setItem('shopping-cart', cartJSON);
}
}
This is how each item look like, and yes, the alert does popup {"key":14759,"id":"1","productName":"Sample Product 1","price":"12.77","quantity":"1"}
'TheCart'(.getItem('TheCart'));) is equal toitemyou're passing in.removeItem(item)itemkey in your Session Storage? It seems you only have the key"TheCart".shoppingCartis the object that hasitem, not the Session Storage. In your Session Storage, you need to set the updated JSON, not remove something.