106

I want to show all of the keys and storage written before. My code is below. I created a function (allStorage) but it doesn't work. How can I do this?

    function storeUserScribble(id) {
        var scribble = document.getElementById('scribble').innerHTML;
        localStorage.setItem('userScribble',scribble);
    }

    function getUserScribble() {
        if ( localStorage.getItem('userScribble')) {
            var scribble = localStorage.getItem('userScribble');
        }
        else {
            var scribble = 'You can scribble directly on this sticky... and I will also remember your message the next time you visit my blog!';
        }
        document.getElementById('scribble').innerHTML = scribble;
    }

    function clearLocal() {
        localStorage.clear();
        return false;
    }

    function allStorage() {
        var archive = [];
        for (var i = 0; i<localStorage.length; i++) {
            archive[i] = localStorage.getItem(localStorage.key(i));
        }
    }
matt2000
  • 1,004
  • 9
  • 17
user1942359
  • 1,219
  • 3
  • 12
  • 8

10 Answers10

130

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;
}
Timo Tijhof
  • 9,867
  • 6
  • 33
  • 47
122

The easiest way in ES2015+ is:

const items = { ...localStorage };
Alexey Prokhorov
  • 2,706
  • 1
  • 19
  • 24
23

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

for (var a in localStorage) {
   console.log(a, ' = ', localStorage[a]);
}
Dziad Borowy
  • 12,045
  • 4
  • 38
  • 51
19

The easiest way would be to use:

return JSON.stringify(localStorage);
Community
  • 1
  • 1
Grumpy
  • 2,061
  • 1
  • 24
  • 36
10
// 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);  

}
Jason
  • 131
  • 1
  • 5
9
for (let [key, value] of Object.entries(localStorage)) {
    console.log(`${key}: ${value}`);
}
Kevin
  • 14,269
  • 7
  • 44
  • 64
Igor Yar
  • 101
  • 2
  • 3
1

A little more succinct:

function getAllLocalStorage() {
    return Object.keys(localStorage)
        .reduce((obj, k) => {
            return { ...obj, [k]: localStorage.getItem(k)}}, {});
        }
itsazzad
  • 6,416
  • 7
  • 67
  • 81
David Burson
  • 2,705
  • 6
  • 30
  • 53
0

To get all of the "localStorage", copy it and set the local storage where you want

var localstorage= {...localStorage};
var content = 'localStorage.clear();';
$.each(localstorage,(i,e)=>{
content+=localStorage.setItem('${i}','${e}');})
copy(content)

Community
  • 1
  • 1
-1

These are all bulky or downright incorrect.

The Correct Answer is:
JSON.parse(JSON.stringify(localStorage))

avalanche1
  • 2,123
  • 1
  • 23
  • 32
-1

Get an array of localStorage keys this way:

console.log('keys: ', localStorage._keys)

Should probably be obvious from trying out the 'Correct Answer', but if you want to iterate through or search just the key names, localStorage._keys is your simple starting point.

Dmitry S.
  • 1,369
  • 1
  • 10
  • 21
JCrist
  • 1
  • 1
    I don't see this working. Did you tried this on Chrome console? or can you share some document where you find localStorage._keys property? – KushalSeth Mar 31 '21 at 20:51
  • Hey @JCrist, welcome to SO! Thank you for the contribution, but your answer does not work. The **localStorage** object does not have a *native property* called **_keys**. – Mac Apr 01 '21 at 01:40