Display JSON Array in HTML

与世无争的帅哥 提交于 2020-01-05 09:07:55

问题


I have a JSON array in localstorage. I want to retrieve this array, and display the values. The storage key is _myobject, and the value is

[{"64508":"12:12:2"},{"5292":"12:17:34"}]

What I want is to know how I can use JavaScript/jQuery to display/print the values in the following format:

Number: 64508 - Time:  12:12:2
Number: 5292 - Time: 12:17:34

Could somebody please direct me on how I could do this?


回答1:


You can use an Array.map to convert from your array into the string. Try something like this:

var obj = JSON.parse(localStorage.getItem("_myobject"));
obj.map(function(item) { 
     // (really just the first key)
     for (var key in item) 
         return "Number: " + key + " - Time: " + item[key]; 
}).join(" ");

(Fiddle)



来源:https://stackoverflow.com/questions/24834818/display-json-array-in-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!