我试图做的是在localstorage
中存储下面的结构localstorage -> hash1 -> {name11, name12, name13, ...}
-> hash2 -> {name21, name22, name23, ...}
...
我将名字收集到一个数组然后按下面推送
files.push(file.name);
localStorage.setItem(hash, JSON.stringify(files));
我认为localstorage就像一个空数组,当我遍历localstorage时,我只会得到我的哈希值。但现在当我循环如下
for(var hash in window.localStorage){
var files = JSON.parse(localStorage.getItem(hash));
var filesHtml= [];
$.each(files, function(i, item) {
filesHtml.push('<li><a href="/' + hash+'/'+item + '">' + item + '</a></li>');
});
...
}
我在localstorage中获得了大量其他数据。我怎样才能收集下面的哈希
localstorage ->history -> hash1 -> {name11, name12, name13, ...}
-> hash2 -> {name21, name22, name23, ...}
...
并在localstorage中创建三维history
数组,这样当我遍历history
时,我只会得到我的哈希,没有其他数据?任何解决方案?
答案 0 :(得分:2)
如何使用数组?
$(document).ready(function(){
var hashes = [];
hashes[0] = ["name1", "name2", "name3"];
localStorage["hashes"] = JSON.stringify(hashes);
var restoredHashes = JSON.parse(localStorage["hashes"]);
for(var i = 0; i < restoredHashes.length; i++){
var names = restoredHashes[i];
for(var j = 0; j < names.length; j++)
{
$("#output").append(names[j] + "<BR/>");
}
}
});
编辑:Redid代码实际工作。查看JSFiddle
编辑:为JSFiddle示例添加了代码,以证明localStorage中保留了“哈希”。查看JSFiddle version2
答案 1 :(得分:1)
我认为这会对你有帮助
for(var hash in window.localStorage){
if(window.localStorage.hasOwnProperty(hash)){
//Do your work
}
}