最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get ID by key in localStorage - Stack Overflow

programmeradmin4浏览0评论

In JS localStorage I can use

localStorage.getItem(key);

to get the value of the entry corresponding to the key in the key variable.

How can I get the entry's ID (instead of value) using the key?


Edit: sorry I must have confused people. What I mean by "key" is the numerical key - which is 0, 1, 2, 3 etc depending on how many items have been saved. Then I want to find out the ID it was stored as, eg foo in the below example, from the numerical key.

localStorage.setItem('foo', 'bar');

In JS localStorage I can use

localStorage.getItem(key);

to get the value of the entry corresponding to the key in the key variable.

How can I get the entry's ID (instead of value) using the key?


Edit: sorry I must have confused people. What I mean by "key" is the numerical key - which is 0, 1, 2, 3 etc depending on how many items have been saved. Then I want to find out the ID it was stored as, eg foo in the below example, from the numerical key.

localStorage.setItem('foo', 'bar');

Share Improve this question edited Sep 5, 2012 at 6:12 asked Sep 5, 2012 at 5:55 user1318194user1318194 3
  • Why do you want to do that? From what I can see in the spec, adding and removing keys may change the order of existing keys, so you can't really count on the numeric index actually meaning anything useful... – nnnnnn Commented Sep 5, 2012 at 6:19
  • I want to populate a <select> menu with DB entries. The <select> menu will be wiped and repopulated each time the program changes something in the DB. – user1318194 Commented Sep 5, 2012 at 6:30
  • Ah, OK. So a for loop with localStorage.getItem(localStorage.key(i)) (where i is obviously the loop counter from 0 to localStorage.length) should do it... – nnnnnn Commented Sep 5, 2012 at 6:48
Add a ment  | 

4 Answers 4

Reset to default 2

LocalStorage is implemented as a key-value pair ( see for instance: https://developers.google./web-toolkit/doc/latest/DevGuideHtml5Storage ) - so you don't have an id like an unique auto-incremented id in a database table.

However, you can access the elements using an index - to get the index of a key in localStorage, the only way I can find is to loop through each key until you find the one you are searching for, like this:

var findIndexOfKey = function(searchKey) {
    for (var i = 0; i < localStorage.length; i++){
        var key = localStorage.key(i);
        if(key === searchKey)
            return i;
    }
    return -1;
}

And then, to retrieve the key using the index, you can do:

localStorage.key(myIndex);

And to retrieve the value, you can do this:

localStorage.getItem(localStorage.key(myIndex));

... or this ( which would be equivalent to localStorage.getItem("myKey")):

localStorage.getItem(localStorage.key(findIndexOfKey("myKey")));

when setting the item you should give it's ID as a value and than when you call getItem(key) it should give it's ID as a return ex:

localStorage.setItem('foo', 'bar');
localStorage.getItem('foo');  // it should return the bar

take a look for this examples it may help : http://mathiasbynens.be/notes/localstorage-pattern

The answer:

localStorage.key(key);

Sorry, I realise I've got confused between what's actually called the key, which I called the ID, and it's numerical ID which I called the key...

I don't think it is possible. Can't you just make localStorage.setItem(yourkey,value)? I mean

localStorage.setItem(0,value)  
localStorage.setItem(1,value)

This may be useful in loops for example.

发布评论

评论列表(0)

  1. 暂无评论