Iterate JavaScript JSON Object - Chrome / Opera Ne

2020-05-07 01:52发布

问题:

First have a look here, with Google Chrome and then FireFox: http://jsbin.com/aherar/1/edit Watch the order of the numbers.

JSON example:

{
     "3259341": {
         "datum": "2013-07-23 14:55:00",
         "titel": "Open Space"
     },
     "3259609": {
         "datum": "2013-07-23 14:50:00",
         "titel": "Something else"
     },
     "3257403": {
         "datum": "2013-07-23 14:45:00",
         "titel": "High-Tech"
     }
 }

If I try to iterate over the JSON-Object with Chrome or Opera Next (WebKit) the "for in" runs reverse over the JSON properties. FireFox or Safari (Desktop or iOS) runs correctly, right order.

Is there a way to do it like expected, like FireFox do?

回答1:

JSON objects are unordered sets of name/value pairs, you can't (and shouldn't!) rely on any certain order of elements.