admin管理员组

文章数量:1330586

I worked a little bit with local storage in browsers. But i only worked with strings as key-value pair. Is there any way to store a list of strings(for a single key) and read them??

For example say I have to display my friends in a page. I actually don't need to query database every time to display the friend list if I can make use of the local storage(i know there will be some inconstancy which is OK for my application). Is there any way to achieve this????

Thank you in advance :)

I worked a little bit with local storage in browsers. But i only worked with strings as key-value pair. Is there any way to store a list of strings(for a single key) and read them??

For example say I have to display my friends in a page. I actually don't need to query database every time to display the friend list if I can make use of the local storage(i know there will be some inconstancy which is OK for my application). Is there any way to achieve this????

Thank you in advance :)

Share Improve this question asked Aug 19, 2013 at 17:26 user2250278user2250278 2
  • 1 If that friends list is forever changing then you're going to want to query the DB :) – tymeJV Commented Aug 19, 2013 at 17:27
  • Couldn't you use an array, or an object to store your list of strings? – j08691 Commented Aug 19, 2013 at 17:28
Add a ment  | 

3 Answers 3

Reset to default 7

Yes - you can. localStorage just stores strings, so you need to store it in a format that can be parsed easily.

Simplest approach for a list of strings would be to store it as a stringified array.

Storing:

localStorage['mylist'] = JSON.stringify(['string1', 'string2']);

Loading:

var mylist = JSON.parse(localStorage['mylist']);

Adding more and storing:

mylist.push('newstring');
localStorage['mylist'] = JSON.stringify(mylist);

You can either store it inside an XML document or simply in any text file using JSON: http://en.wikipedia/wiki/JSON

You can convert the array to JSON. That's one choice. You convert and then store as a string and then read it back and convert back.

For this I use a couple of functions which are below and they are called like this:

jsonStore.putJson("friendInfo", frientInfo); // friendInfo is an object or array

var readFriendInfo = jsonStore.getJson("friendInfo");

var jsonStore = function () {
    return {
        getJson: function(key) {
            var ret = localStorage[key];
            if (ret) {
                try {
                    ret = JSON.parse(ret); // use JSON3 to handle multiple browsers/versions
                } catch (e) {
                    ret = undefined; // for parse failure
                }
            }
            return ret;
        },
        putJson: function(key, value) {
            localStorage[key] = JSON.stringify(value); // use JSON3 for this too
        }
    };
}();

Also

You could do something like this, assuming the string about each friend is in an array element, friendInfo with each index indicating a different friend.

for (var i=0; i < friendInfo.length ; ++i) {
    localStorage.setItem("friend" + i, firentInfo[i]);
}

When I do this, I usually store another item that tells how many there are:

localStorage.setItem("friend.length", friendInfo.length);

本文标签: javascriptStoring a list of strings in local storageStack Overflow