Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Retrieve all keys from sessionStorage?

Is it not posssible to retrieve all keys/objects that I stored in sessionStorage (or localStorage)?

If I have done sessionStorage.name = 'John' and sessionStorage.city = 'New York', isn't there a way to get a list that shows the keys name and city?

like image 999
TheStoryCoder Avatar asked Jan 24 '17 10:01

TheStoryCoder


2 Answers

to get a list that shows the keys name and city

Use Object.keys() function:

sessionStorage.name = 'John';
sessionStorage.city = 'New York';

console.log(Object.keys(sessionStorage));
like image 147
RomanPerekhrest Avatar answered Sep 18 '22 13:09

RomanPerekhrest


With this you can assign all the data in local storage into an object:

let storage = {}
Object.keys(localStorage).forEach((key) => {
    storage[key] = localStorage.getItem(key);
});
like image 42
Matthew Perlman Avatar answered Sep 20 '22 13:09

Matthew Perlman