从LocalStorage中检索所有数据(不知道密钥名称) [英] Retrieve all data from LocalStorage (without knowing the key name)

查看:145
本文介绍了从LocalStorage中检索所有数据(不知道密钥名称)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法从localStorage中获取所有信息。我遇到的麻烦是我不知道数据会是什么,因为它是用户生成的。

所以在这里发生了什么,用户输入了一些文本,它使用javascript来操作它,具体取决于他们在输入表单上勾选了哪些复选框。这些框是用于符号的,例如,如果他们在@框中打勾,那么文本+ @At(符号然后是词)将被放置在本地存储器中,另一半作为布尔值(在这种情况下为1或0)表示它是否被检查过。



确切的一对将如下所示:



someString .. @ At | 1
someString ..#Hash | 0





还应该注意,这是为了在Chrome扩展中使用在其他浏览器中的兼容性对我来说不是必需的(尽管其他人阅读这些内容可能会很有用,因为我无法在网上找到其他任何内容)。



因此,如果无论如何我可以提取localStorage中的所有值,而无需真正知道每个键的名称?
是否有可能使用任何类型的通配符或正则表达式可能,我已经尝试过,但应使其使用for循环。



谢谢,
Wez

解决方案

window.localStorage.key是解决方案。
示例:

  var i = 0,
oJson = {},
sKey;
for(; sKey = window.localStorage.key(i); i ++){
oJson [sKey] = window.localStorage.getItem(sKey);
}
console.log(oJson);


I'm looking for a way to get all the information out of localStorage. The trouble I'm having is I don't know what the data will be as it is user generated.

So here what happens, a user inputs some text, it uses javascript to manipulate it depending on what check boxes they have ticked on the input form. these boxes are for symbols for example if they tick the box for @ then the text + the @At (symbol then word) will be placed in local storage with the other half of the pair as a Boolean (1 or 0 in this case) representing whether its been checked.

the exact pair would look like this:

someString..@At        | 1
someString..#Hash     | 0

etc.

It should also be noted that this is intended to be used in a Chrome Extension so compatibility in other browsers is not a requirement for me (although it could well be usful to others reading this as I can't find anything else covering it on the web).

So, if there anyway I can extract all the values in localStorage without actually knowing the name of each key? Is it possible to use any kind of wild card or regular expression maybe, I have tried this but should make it work using a for loop.

Thanks, Wez

解决方案

window.localStorage.key is the solution. Example:

var i = 0,
    oJson = {},
    sKey;
for (; sKey = window.localStorage.key(i); i++) {
    oJson[sKey] = window.localStorage.getItem(sKey);
}
console.log(oJson);

这篇关于从LocalStorage中检索所有数据(不知道密钥名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆