Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove data which is stored in local storage?

If I console.log(localStorage.getItem("cartCache")), the result like this :

{"dataCache":[{"id":20,"quantity":1,"total":100000,"request_date":"27-08-2017 20:31:00"},{"id":53,"quantity":1,"total":200000,"request_date":"27-08-2017 20:38:00"}]}

I want to remove the data in the cache by id

For example, I remove id = 20, it will remove id = 20 in the cache

So the result to be like this :

{"dataCache":[{"id":53,"quantity":1,"total":200000,"request_date":"27-08-2017 20:38:00"}]}

How can I do it?

like image 726
moses toh Avatar asked Dec 18 '25 11:12

moses toh


1 Answers

You need to retrieve the object modify it and then store it again in local storage,

var retrievedObj = JSON.parse(localStorage.getItem("cartCache"));
retrievedObj.dataCache[0].id = 53;
localStorage.setItem('cartCache', JSON.stringify(retrievedObj));
like image 169
Sajeetharan Avatar answered Dec 21 '25 01:12

Sajeetharan