Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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));


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...