localStorage different keys
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 listoption1
variable 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);
精彩评论