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.
setItem() – the setItem method is used to add data to a web storage object. It takes in two arguments, a key and value pair, window.localStorage.setItem("key", value)
getItem() – the getItem method returns the value of the key name that’s passed to it, such as window.localStorage.getItem("key name")
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.
Смотрите видео How to store objects in HTML5 localStorage онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Quick Code Academy 24 Декабрь 2022, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 286 раз и оно понравилось 7 людям.