filmov
tv
How to store objects in HTML5 localStorage

Показать описание
Let’s look at how to store JavaScript objects in the localStorage object using the setItem and getItem methods, as well as two JSON methods, stringify and parse.
JSON.stringify – the JSON.stringify method converts any object or acceptable value into a string JSON
JSON.parse – the JSON.parse method converts a string into its corresponding object or value as described by the string
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! That also means the object must be run through JSON.
JSON.stringify – the JSON.stringify method converts any object or acceptable value into a string JSON
JSON.parse – the JSON.parse method converts a string into its corresponding object or value as described by the string
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! That also means the object must be run through JSON.