Skip to content
Advertisement

Local Storage is overwritten instead of the new elements being added (JavaScript)

I want to storage an array of objects in local storage but I just can storage one object into the array. All the new objects are overwriting the previous one in index 0.

This is my JavaScript code:

JavaScript

Thanks in advance!!

Advertisement

Answer

get the current content of the local storage first and then put the new objects into the array.

JavaScript

EDIT: if the current objects that has the same ID as the new object need to be replaced, the new array needs to be adjusted.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement