简体   繁体   中英

How to delete last added element from localstorage json array

I have tried the below code to delete last added array(Users array)object.It works in console.but it doesnt delete the data from localstorage.Any help would be appreciated

var delRecord = JSON.parse(localStorage.getItem("MyRecords"));
for (var d = 0; d < delRecord .length; d++) {
        delRecord [d].Users.pop();          
}

Is pop() method will work or any suggestions?

You need to write the result back to the local storage:

var delRecord = JSON.parse(localStorage.getItem("MyRecords"));
for (var d = 0; d < delRecord .length; d++) {
        delRecord[d].Users.pop();          
}

localStorage.setItem("MyRecords", JSON.stringify(delRecord));

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