Remove Json Object In Localstorage Using Js
[{'id':1,'content':'something'},{'id':2,'content':'something diff'},{'id':3,'content':'something diff'}] by localStorage.getItem('data') I got the above json object, but how to de
Solution 1:
Assuming you've JSON.parse'd your local storage data into an array, you can remove the second item like you would from any other array- by popping it off.
vardata = localStorage.getItem('data');
// At this point, data is either null or a string value.// To restore the string to an Array you need to use JSON.parseif (data) {
data = JSON.parse(data);
// At this point you can use Array methods like pop or splice to remove objects.
}
// At this point, your Array will only contain the first item.// If you want to write it back to local storage, you can like so:// Be sure to use JSON.stringify so it can later be restored with parse.
localStorage.setItem('data', JSON.stringify(data));
Solution 2:
This will turn "data" into a javascript object and remove the last item (assuming this is always an array):
vardata = localStorage.getItem('data');
if (data) {
data = JSON.parse(data);
data.pop();
}
Post a Comment for "Remove Json Object In Localstorage Using Js"