Answers for "how to store and extract local storage"

0

how to store and extract local storage

let p1 = new Point(1,2);
let p2 = new Point(4,8);
let p3 = new Point(3,7);
// create a polygon using an array containing p1, p2, p3
let poly1 = new Polygon([p1,p2,p3]);

// store in local storage
localStorage.setItem("poly1",JSON.stringify(poly1));

// retrieve into new variable
let poly1Data = JSON.parse(localStorage.getItem("poly1"));
Posted by: Guest on April-25-2021
0

storing and extracting from local storage

/* Storing object data in local storage */
let objectData = {
    "data": "this is a line of text",
    "anArray": [0,1,2,3,4,5],
    "aBoolean": true
}
// convert object into a string
let jsonString = JSON.stringify(objectData);
// put it in local storage
localStorage.setItem("storage_key",jsonString);
// we also can use a variable to store the key value instead of using a string directly
let key = "storage_key";
localStorage.setItem(key,jsonString);

/* Retrieving object data from local storage */
let key = "storage_key"; // our key
// get the item from localStorage
let jsonString = localStorage.getItem(key);
// convert it back into an object
let objectData = JSON.parse(jsonString);
Posted by: Guest on April-23-2021

Code answers related to "how to store and extract local storage"

Code answers related to "Javascript"

Browse Popular Code Answers by Language