开发者

localStorage different keys

开发者 https://www.devze.com 2023-03-27 14:20 出处:网络
I have the following: localStorage.setItem(\"list\",listoption1); localStorage.setItem(\"list\",listoption2);

I have the following:

localStorage.setItem("list",listoption1);
localStorage.setItem("list",listoption2);
// ...
localStorage.setItem("list",listoption10);

I also have:

localStorage.setItem("settings",开发者_如何学Csettingval);

I want to list all list options, but when I ask for localStorage.key(i) I also receive the value from the settings key. Is it possible to get everyting stored in list key only?


The localStorage is a key-value storage. So storing key list with value of listoption1 will store the value of listoption1variable to the key list. The next method will overwrite this key with value of listoption2.

Fetching list with localStorage.getItem('list') will return the value of listoption2, so there is no way to fetch all list options because the latter will overwrite the preceding.

Update:

If you want to save a Hash (Dictionary, Object, ...) to your key. Then you need to encode it somehow. I would choose JSON.

Example:

localStorage.setItem("list", JSON.stringify({ listoption1: listoption1, listoption2: listoption2 }));
var options = JSON.deceode(localStorage.getItem("list"));
alert(options.listoption1);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号