简体   繁体   中英

How to retrieve multiple data from localStorage at a time and store the data

How do I get all the different key/value pairs stored in the localstorage whenever my window reloads using vanilla JS.And also How to store these data in a object with similar properties which I receive from the localStorage ? for eg. : if I have thses 2 things in local storage

localStorage.setItem("todoList","Task 1");
localStorage.setItem("doingList","Task 1");

You can only put one value in a localStorage key.

Put the values in an array, convert the array to JSON, and store that.

var todoList = ["Task 1","Task 2","Task 3"];
localStorage.setItem("todoList", JSON.stringify(todoList));

To retrieve it you call JSON.parse()

todoList = JSON.parse(localStorage.getItem("todoList") || "[]");

Just destruct the localStorage and get all Items

const items = { ...localStorage };
console.log(items)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM