Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 打印本地存储项的概述_Javascript_Html_Local Storage - Fatal编程技术网

Javascript 打印本地存储项的概述

Javascript 打印本地存储项的概述,javascript,html,local-storage,Javascript,Html,Local Storage,我正试图在我的html页面上打印所有localstorage项目的良好概述。为了实现这一点,我进行了以下循环: for (var i = 0; i < localStorage.length; i++){ var entry = localStorage.getItem(localStorage.key(i)); $('body').append('<article rel="'+entry[0]['key']+'"><h2>'+entry[0]['

我正试图在我的html页面上打印所有localstorage项目的良好概述。为了实现这一点,我进行了以下循环:

for (var i = 0; i < localStorage.length; i++){
    var entry = localStorage.getItem(localStorage.key(i));
    $('body').append('<article rel="'+entry[0]['key']+'"><h2>'+entry[0]['title']+'</h2><img class="entry_img" src="'+entry[0]['image']+'"><p><span class="actions"><img class="delete" src="img/deleteicon.png" /></span>'+entry[0]['content']+'<span class="time">Toegevoegd op:'+entry.[0]['date']+'</span>'+entry.[0]['location']+'</p></article>');
}

嗯,您的代码中有语法错误,请看:
entry.[0]['date']
,它应该是
entry[0]['date']
(不带点)


对于
条目也存在相同的错误。[0]['location']
,请删除该点。

localStorage
是一组字符串键/值对,因此
条目将始终是一个字符串

因此,您可能有以下情况:

var entry = localStorage.getItem(localStorage.key(i)); // Returns "example"
entry[0] // Returns "e"
entry[0]['key'] // Returns undefined
如果需要将对象存储在本地存储中,则需要首先使用类似于JSON.stringify的东西对其进行字符串化

从您的编辑:看起来您已经有了一个JSON字符串,所以您需要做的就是解析它:

for (var i = 0; i < localStorage.length; i++){
    var entry = JSON.parse(localStorage.getItem(localStorage.key(i))); // Parse the string value
    $('body').append('<article rel="'+entry[0]['key']+'"><h2>'+entry[0]['title']+'</h2><img class="entry_img" src="'+entry[0]['image']+'"><p><span class="actions"><img class="delete" src="img/deleteicon.png" /></span>'+entry[0]['content']+'<span class="time">Toegevoegd op:'+entry.[0]['date']+'</span>'+entry.[0]['location']+'</p></article>');
}
for(var i=0;i');
}

谢谢!这似乎解决了我的部分问题。现在我得到了这个错误:[error]TypeError:“undefined”不是一个对象(评估“entry[0]['title'])@frankluytmans
entry
是一个对象,而不是数组。因此,您可以不使用数组索引器进行访问,例如
entry.title
Yes。我的错!谢谢你帮助我:-)
for (var i = 0; i < localStorage.length; i++){
    var entry = JSON.parse(localStorage.getItem(localStorage.key(i))); // Parse the string value
    $('body').append('<article rel="'+entry[0]['key']+'"><h2>'+entry[0]['title']+'</h2><img class="entry_img" src="'+entry[0]['image']+'"><p><span class="actions"><img class="delete" src="img/deleteicon.png" /></span>'+entry[0]['content']+'<span class="time">Toegevoegd op:'+entry.[0]['date']+'</span>'+entry.[0]['location']+'</p></article>');
}