I add list to sessionStorage like:
var item = new cartItem(product.id, product.name, product.price, qty);
orderItem.push(item);
sessionStorage.setItem('addedProductsList', JSON.stringify(orderItem));
var retrieveArray= JSON.parse(sessionStorage.addedProductsList);
and its working fine, now i want to remove a single object from this list by productId.
Please, see the following article: http://www.nczonline.net/blog/2009/07/21/introduction-to-sessionstorage/
If you want to remove specify key/value pair from session storage, you need smth like this:
sessionStorage.removeItem(key)
For your case:
var retrieveArray= JSON.parse(sessionStorage.addedProductsList);
for (i=0; i<retrieveArray.length; i++){
if (retrieveArray[i].id == "Your ProductId") {
retrieveArray.splice(i,1);
}
}
sessionStorage.addedProductsList = retrieveArray;
Additionally, proper implementations allow you to read, write, and remove values from sessionStorage as if it were a regular object. For example:
//save a value
sessionStorage.name = "Name";
//retrieve item
var name = sessionStorage.name;
//remove the key
delete sessionStorage.name;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With