Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

javascript - remove item at a specific position in localStorage

Hi i want to remove from the localStorage a specific item at a specific position. If i have this JSON:

key: user_login
0: {
password: "aaaa", 
indirizzo: "via dei mille"
}

1: {
password: "kkkk",
indirizzo: "via garibaldi 6"}

Now i want delete JSON at position 0(password: "aaaa", indirizzo: "via dei mille"). I try:

var storedNames = localStorage.getItem("user_login_users");
arr1.slice(0,1);

But localStrorage don't remove nothing. With localstorage.removeitem(key) i can't remove all item at position 0. How can i do?

question from:https://stackoverflow.com/questions/65648569/remove-item-at-a-specific-position-in-localstorage

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You've retrieved the values from storage, you've then edit that value in-memory.
You'll then have to persist these changes, by writing them to the storage.

var storedNames = JSON.parse(localStorage.getItem("user_login_users"));
// remove an entry
var newStoredNames = storedNames.slice(0,1);
// or change an existing one.
newStoredNames[0].password="my_new_value";
// persist the changes:
localStorage.setItem("user_login_users", JSON.stringify(newStoredNames));


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...