Iterate over jQuery JSON object in original order

前端 未结 4 1931
误落风尘
误落风尘 2021-01-04 09:13

I\'ve got some json data in a map object which is sorted by time. The key is an integer id and the value is an object which contains a timestamp. However when I try to ite

4条回答
  •  孤独总比滥情好
    2021-01-04 09:48

    Objects are un-ordered sets. You can't specify an order on them in a cross-browser complaint manner. There is no concept of original order unless it's already ordered by a rule.

    So sort the data on the server and then enumerate over it in the same sorted order.

    Alternatively format the JSON to be

    { "values": [
       { "someKey": "someVal" },
       { "someOtherKey": "someOtherVal" }
    ] }
    

    You can iterate over the array with a for (i = 0; i < len; i++) loop and "garantuee" the original order.

提交回复
热议问题