jquery sorts my date automatically

1

I generate a json in any url, like this:

{"130":"chocolate","129":"banana","120":"chiclete"}

I'll get it with $ .get

$.get(url,function(data){
   console.debug(data);
},'json');

result (it sorts automatically, without me needing it):

Object { 120="chiclete", 129="banana", 130="banana"}
    
asked by anonymous 31.07.2014 / 01:02

1 answer

1

In JavaScript, objects are sets of key / value pairs. Sets, by definition, have no order. {1,2,3} == {1,3,2} == {2,1,3} etc. At the time of printing to the console, an arbitrary choice of an order (which, in this case, was in ascending order of the keys), but that does not mean that the object is actually ordered, nor that in all situations the order of iteration / use / print will be the same.

If you really need a defined order - be the order the server sent it to, any ordering you've defined via JavaScript - so do not use objects, use array arrays: p>

[["130","chocolate"],["129","banana"],["120","chiclete"]]

Otherwise, the browser can and will change the [usage] order as desired.

    
31.07.2014 / 08:25