Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove single object from the sessionStorage (AngularJs)

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.

like image 397
DIGAMBAR TOPE Avatar asked Dec 25 '22 01:12

DIGAMBAR TOPE


1 Answers

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;
like image 172
Artyom Pranovich Avatar answered Dec 28 '22 06:12

Artyom Pranovich