4

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.

1 Answer 1

10

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;
Sign up to request clarification or add additional context in comments.

1 Comment

@DIGAMBARTOPE, you are welcome! But if it was helpful for you, you can accept this answer as right ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.