I have a simple to-do list app and I want to save/load the added items. I’m using the following code to add an item to localStorage each time an item is added:
localStorage.setItem("LIST_ITEM_" + makeid(24), uinput); //makeid() generates random strings
I’m setting the key to LIST_ITEM_ and a random 24-character string
I am using the following code to load items from localStorage:
for (i in localStorage) { // Iterate through the cookies
for (const [key,value] of Object.entries(localStorage)) { // Create key-value pairs for each
if (key.includes("LIST_ITEM")) { // Check if the key is a LIST_ITEM
// Create elements to display the item
}
}
}
The problem comes in on the page where it seems to load each item 8 times instead of just once as it should.
I ran this code to see what was inside localStorage:
for (i in localStorage) {console.log(localStorage[i])}
And got this:
ww
VM1809:1 w
VM1809:1 2
VM1809:1 ƒ clear() { [native code] }
VM1809:1 ƒ getItem() { [native code] }
VM1809:1 ƒ key() { [native code] }
VM1809:1 ƒ removeItem() { [native code] }
VM1809:1 ƒ setItem() { [native code] }
The keys that have actually been input are “w,” and “ww”.
The page displays:
w
ww
8 times.
Not sure what I’ve done wrong but that is definitely not supposed to happen.