如何遍历下面的本地存储,然后在我的数组中写出来?
本地存储看起来像这样:
{"data":[[0,"Post1","Text1","2016-12-16T11:01:00.000Z"],[1,"Post2","Text2","2016-12-20T14:00:00.000Z"]],[3,"Post3","Text3","2016-12-25T13:00:00.000Z"]]}
我可以这样读:
info = JSON.parse(localStorage.getItem("rp_data"));
var obj = [];
for(var i = 0; i < info.data.length; i++){
var data = info.data[i];
obj.push({title : data[1], message : data[2], date : data[3]})
}
console.log(JSON.stringify(obj))//this output is right
好的,现在JSON.stringify(obj)是正确的,现在我想在一个新数组中显示它: 所以我尝试了这个:
var html=(JSON.stringify(obj))
items:
html
,
已经解决了,我只需要使用obj而不是用它来串行: - )
答案 0 :(得分:0)
以下是具有多个阵列的解决方案。
var tmp = {"data":[[0,"Post1","Text1","2016-12-16T11:01:00.000Z"] , [1,"Post1","Text2","2016-12-16T11:01:00.000Z"], [1,"Post1","Text3","2016-12-16T11:01:00.000Z"] ]};
var obj = [];
for(var i = 0; i < tmp.data.length; i++){
var data = tmp.data[i];
obj.push({title : data[1], message : data[2], date : data[3]})
}
console.log(JSON.stringify(obj))
输出结果为:
[{"title":"Post1","message":"Text1","date":"2016-12-16T11:01:00.000Z"},{"title":"Post1","message":"Text2","date":"2016-12-16T11:01:00.000Z"},{"title":"Post1","message":"Text3","date":"2016-12-16T11:01:00.000Z"}]