String Set In Localstorage Gets As Object
I've JSON.stringified objects to save in localStorage, but they're retrieved as objects ( '[object Object]' ) that can't be parsed. Scenario: The first thing I do is localStorage.c
Solution 1:
You need to stringify an object before storing it:
localStorage.setItem('key', JSON.stringify(obj));
Then, when retrieving it, you just do
JSON.parse(localStorage.getItem('key'));
The reason for this is that everything is stored as a string, so it calls the .toString() method, which for objects returns [object Object]
EDIT I noticed you said you did stringify the objects. I guess it would depend the format of the object then, but I haven't encountered any issue with not clearing storage beforehand.
Post a Comment for "String Set In Localstorage Gets As Object"