Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve all localStorage items without knowing the keys in advance?

People also ask

How do I get all items from localStorage?

To get items from localStorage, use the getItem() method. getItem() allows you to access the data stored in the browser's localStorage object.

How do I see my localStorage items?

To check if a key exists or not in localStorage, we can use the localStorage. getItem() method. The localStorage. getItem() method takes the key as an argument and returns the key's value.

Can localStorage be hacked?

If an attacker can run JavaScript on your website, they can retrieve all the data you've stored in local storage and send it off to their own domain. This means anything sensitive you've got in local storage (like a user's session data) can be compromised.


If you modify your function to this you can list all items based on key (will list the items only):

function allStorage() {

    var values = [],
        keys = Object.keys(localStorage),
        i = keys.length;

    while ( i-- ) {
        values.push( localStorage.getItem(keys[i]) );
    }

    return values;
}

Object.keys is a new addition to JavaScript (ECMAScript 5). It lists all own keys on an object which is faster than using a for-in loop which is the option to this.

However, this will not show the keys. For that you need to return an object instead of an array (which is rather point-less IMO as this will bring you just as far as you were before with localStorage just with a different object - but for example's sake):

function allStorage() {

    var archive = {}, // Notice change here
        keys = Object.keys(localStorage),
        i = keys.length;

    while ( i-- ) {
        archive[ keys[i] ] = localStorage.getItem( keys[i] );
    }

    return archive;
}

If you want a compact format listing then do this instead - here each item in the array will have key=item which you later can split into pairs and so forth:

function allStorage() {

    var archive = [],
        keys = Object.keys(localStorage),
        i = 0, key;

    for (; key = keys[i]; i++) {
        archive.push( key + '=' + localStorage.getItem(key));
    }

    return archive;
}

The easiest way in ES2015+ is:

const items = { ...localStorage };

localStorage is not an array, but an object, so try sth. like this:

for (var a in localStorage) {
   console.log(a, ' = ', localStorage[a]);
}

The easiest way would be to use:

return JSON.stringify(localStorage);

// iterate localStorage
for (var i = 0; i < localStorage.length; i++) {

  // set iteration key name
  var key = localStorage.key(i);

  // use key name to retrieve the corresponding value
  var value = localStorage.getItem(key);

  // console.log the iteration key and value
  console.log('Key: ' + key + ', Value: ' + value);  

}

for (let [key, value] of Object.entries(localStorage)) {
    console.log(`${key}: ${value}`);
}

A little more succinct:

function getAllLocalStorage() {
    return Object.keys(localStorage)
        .reduce((obj, k) => {
            return { ...obj, [k]: localStorage.getItem(k)}}, {});
        }