storing and extracting from local storage

Solutions on MaxInterview for storing and extracting from local storage by the best coders in the world

showing results for - "storing and extracting from local storage"
Brogan
30 Oct 2019
1/* Storing object data in local storage */
2let objectData = {
3    "data": "this is a line of text",
4    "anArray": [0,1,2,3,4,5],
5    "aBoolean": true
6}
7// convert object into a string
8let jsonString = JSON.stringify(objectData);
9// put it in local storage
10localStorage.setItem("storage_key",jsonString);
11// we also can use a variable to store the key value instead of using a string directly
12let key = "storage_key";
13localStorage.setItem(key,jsonString);
14
15/* Retrieving object data from local storage */
16let key = "storage_key"; // our key
17// get the item from localStorage
18let jsonString = localStorage.getItem(key);
19// convert it back into an object
20let objectData = JSON.parse(jsonString);
21
Alexandra
26 Aug 2018
1let p1 = new Point(1,2);
2let p2 = new Point(4,8);
3let p3 = new Point(3,7);
4// create a polygon using an array containing p1, p2, p3
5let poly1 = new Polygon([p1,p2,p3]);
6
7// store in local storage
8localStorage.setItem("poly1",JSON.stringify(poly1));
9
10// retrieve into new variable
11let poly1Data = JSON.parse(localStorage.getItem("poly1"));