localStorage不同的key
我有以下内容:
localStorage.setItem("list",listoption1);
localStorage.setItem("list",listoption2);
// ...
localStorage.setItem("list",listoption10);
我还有:
localStorage.setItem("settings",settingval);
我想列出所有列表选项,但是当我请求 localStorage.key(i)
时,我还会收到来自设置键的值。是否可以仅将所有内容存储在列表键中?
I have the following:
localStorage.setItem("list",listoption1);
localStorage.setItem("list",listoption2);
// ...
localStorage.setItem("list",listoption10);
I also have:
localStorage.setItem("settings",settingval);
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
localStorage
是一个键值存储。因此,存储值为listoption1
的键list
会将listoption1
变量的值存储到键list
中。下一个方法将使用listoption2
的值覆盖此键。使用
localStorage.getItem('list')
获取list
将返回listoption2
的值,因此无法获取所有列表选项,因为后者将覆盖前面的。更新:
如果您想将哈希(字典、对象...)保存到您的密钥中。然后你需要以某种方式对其进行编码。我会选择 JSON。
例子:
The
localStorage
is a key-value storage. So storing keylist
with value oflistoption1
will store the value oflistoption1
variable to the keylist
. The next method will overwrite this key with value oflistoption2
.Fetching
list
withlocalStorage.getItem('list')
will return the value oflistoption2
, 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: