Display JSON Array in HTML

2019-07-31 18:21发布

问题:

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)