Javascript: Retrieve all keys from sessionStorage?
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);
});
You can use the Object.keys()
and reduce()
:
var obj = Object.keys(localStorage).reduce(function(obj, key) {
obj[key] = localStorage.getItem(key);
return obj
}, {});
console.log(obj)
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));