Updating Localstorage Arrays In Javascript
I'm trying to store and update an array in the localstorage using JSON.parse/stringify. But it doesn't seem to be working. yesArray = JSON.parse(localStorage.getItem(yesArray))
Solution 1:
This seems to be the problem with passing the key of local storage without quotes.
While reading from local storage use the key as argument as it stores the value as key/value pairs.
yesArray = JSON.parse(localStorage.getItem("yesArray"));
Solution 2:
Missing quotes around yesArray
in the first line?
yesArray = JSON.parse(localStorage.getItem('yesArray'));
Sample:
var yesArray = [];
localStorage.setItem('yesArray', JSON.stringify(yesArray));
yesArray = JSON.parse(localStorage.getItem('yesArray'));
yesArray.push('yes');
localStorage.setItem('yesArray', JSON.stringify(yesArray));
JSON.parse(localStorage.getItem('yesArray')); // Returns ["yes"]
Post a Comment for "Updating Localstorage Arrays In Javascript"