Is the order of elements in a JSON list preserved?

后端 未结 5 905
半阙折子戏
半阙折子戏 2020-11-22 12:20

I\'ve noticed the order of elements in a JSON object not being the original order.

What about the elements of JSON lists? Is their order maintained?

5条回答
  •  天涯浪人
    2020-11-22 12:52

    Practically speaking, if the keys were of type NaN, the browser will not change the order.

    The following script will output "One", "Two", "Three":

    var foo={"3":"Three", "1":"One", "2":"Two"};
    for(bar in foo) {
        alert(foo[bar]);
    }
    

    Whereas the following script will output "Three", "One", "Two":

    var foo={"@3":"Three", "@1":"One", "@2":"Two"};
    for(bar in foo) {
        alert(foo[bar]);
    }
    

提交回复
热议问题