0

Possible Duplicate:
HTML5 local storage sort

I am storing data into a localstorage using HTML5 I need to have it sort in the order I put them in. Originally, I had them start with Q: and then appended data to it. I found out taht when I retrieved the data, it did not come out in the same order I entered them in. I then did someting like 1Q:.., 2Q:.. + ..

but found that it again did not sort in the order I needed them in.

      localStorage.getItem(... )

Wondering how I can tell it what sort order to retrieve the data in?

I have the key as such:

1Q:fdf
2Q:dfdf
3Q:dfdf

For the value is is random values. I as hoping that having the key in a certain order may make it work.

4

1 回答 1

1

localStorage 是一个纯键值存储。它不关心订单或除了它的键值对之外的任何东西,它们总是被字符串化的。

如果您想做一些更花哨的事情,您可能想使用indexed db现在属于 html5 标准的(或web sql database在某些浏览器中实现但前一段时间被放弃的)或传递可能包含更多信息的字符串化对象,例如值和可能的排序顺序。

您究竟是如何将数据传递到本地存储的?

编辑您对我的问题的回答:

您可以创建一个数组,包含您的排序值并存储它的字符串化表示。您可能需要为此编写自己的排序函数。

localStorage.setItem(JSON.stringify( <your sorted array> ));

然后,要检索该值,您可以执行以下操作:

var array = JSON.parse( localStorage.getItem( <your sorted array> ) );
于 2012-04-06T19:02:26.813 回答