JSON.stringify()
MethodBy default, the localStorage or sessionStorage only allows you to store string key/value pairs. But you can also store the JavaScript objects in web storage with a little trick.
To store objects first stringify it using the JSON.stringify()
method, and later parse it with the JSON.parse()
when you need retrieve it, as shown in the following example:
<script>
var personObject = { name: "Peter", age: 18, married: false };
// Convert the person object into JSON string and save it into storage
localStorage.setItem("personObject", JSON.stringify(personObject));
// Retrieve the JSON string
var jsonString = localStorage.getItem("personObject");
// Parse the JSON string back to JS object
var retrievedObject = JSON.parse(jsonString);
console.log(retrievedObject);
// Accessing individual values
console.log(retrievedObject.name); // Prints: Peter
console.log(retrievedObject.age); // Prints: 18
console.log(retrievedObject.married); // Prints: false
</script>
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to add or remove package in Laravel application
When you upgrade your Laravel applicatio...Laravel User Email Verification and Account Activation Example
Today, Laravel is a most popular PHP fra...How to Check If the Mouse is Over an Element in jQuery
Use the CSS :hover Pseudo-class You c...Laravel 8 WHERE Like Query Example Tutorial
When you put the search form in your app...Laravel 5.5 - VueJS 2.0 CRUD Operations
Today, we are share with you VueJS 2.0 C...