简体   繁体   中英

How to remove an item from an array of localstorage?

I've set few localstorage in my site with different names. namely:

localstorage.setItem('one');
localstorage.setItem('two');
//console.debug(localstorage.getItem('two'));[2,5,6]

Now, the localstorage.two is array. How to remove a value from this particular localstorage?

I tried:

localstorage.removeId(2);
localstorage.removeId('two',2);

In localStorage can not be saved Arrays. So we need use JSON.parse and JSON.stringify.

localStorage.setItem("arr",JSON.stringify(["value1","value2"]));

//get element

var element1=JSON.parse(localStorage.getItem("arr"))[0];

console.log(element1);

//remove element

var arr=JSON.parse(localStorage.getItem("arr"));
arr.splice(0,1);//remove first

localStorage.setItem("arr",JSON.stringify(arr));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM