Local storage can only save strings, so storing objects requires that they be turned into strings using JSON. stringify - you can't ask local storage to store an object directly because it'll store “[object Object]”, which isn't right at all!
What can be stored in localStorage?
LocalStorage is a key/value datastore that's available on a user's browser. Like cookies, LocalStorage can only store string data for its keys and values. The datastore is only accessible to JavaScript within that domain. Note: Each domain has access to its LocalStorage datastore.
How do I add an object to localStorage?
There are two options:
- Instead of storing the object store a list/map of objects, then to add an element just first do the getItem , then push/set the new element, then use setItem .
- Store the objects using the date as the key (e.g. localStorage. setItem('obj:' + x.