0

Responses from jquery is corrupted

Somehow the response.data.pool.account assoc array is ordered by the keys?!

jquery

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

code

var success = function(response){
    console.log(response.data.pool.account);
};

response in console (firebug)

{"data":{"pool":{"account":{"176":{"account_id_":"999"},"169":{"account_id_":"1110"},"189":{"account_id_":"1120"},"190":{"account_id_":"1130"},"173":{"account_id_":"2100"},"188":{"account_id_":"2200"},"184":{"account_id_":"3010"},"185":{"account_id_":"3020"},"186":{"account_id_":"4010"},"187":{"account_id_":"4020"},"179":{"account_id_":"5000"},"181":{"account_id_":"9999"},"167":{"account_id_":"14261"},"168":{"account_id_":"14262"},"172":{"account_id_":"15000"},"171":{"account_id_":"15261"},"170":{"account_id_":"15262"},"177":{"account_id_":"15500"},"174":{"account_id_":"16000"},"182":{"account_id_":"16500"},"175":{"account_id_":"17000"},"183":{"account_id_":"17500"},"180":{"account_id_":"19999"}}}}}

console outout in jquery success method

167
    Object { account_id_=14261}
168
    Object { account_id_=14262}
169
    Object { account_id_=1110}
170
    Object { account_id_=15262}
171
    Object { account_id_=15261}
172
    Object { account_id_=15000}
173
    Object { account_id_=2100}
174
    Object { account_id_=16000}
175
    Object { account_id_=17000}
176
    Object { account_id_=999}
177
    Object { account_id_=15500}
179
    Object { account_id_=5000}
180
    Object { account_id_=19999}
181
    Object { account_id_=9999}
182
    Object { account_id_=16500}
183
    Object { account_id_=17500}
184
    Object { account_id_=3010}
185
    Object { account_id_=3020}
186
    Object { account_id_=4010}
187
    Object { account_id_=4020}
188
    Object { account_id_=2200}
189
    Object { account_id_=1120}
190
    Object { account_id_=1130}
clarkk
  • 24,753
  • 63
  • 173
  • 296
  • what do you mean by corrupted? Not sorted in order? – kushyar Jun 02 '13 at 10:02
  • the order is changed compared to the JSON response from the server – clarkk Jun 02 '13 at 10:02
  • Further to zerkms's answer, how were you trying to use an object's properties such that you though you would get them in some particular order? (That's what arrays are for, so if order is important restructure your JSON to use an array of objects.) – nnnnnn Jun 02 '13 at 10:05
  • "assoc array" --- it's not an array, it's a dictionary – zerkms Jun 02 '13 at 10:07
  • 1
    http://json.org/: "*An object is an **unordered** set of name/value pairs*". And it's the same in JavaScript – Bergi Jun 02 '13 at 11:59

1 Answers1

2

Javascript object doesn't guarantee any particular order of its keys.

It's up to js engine to layout properties in memory as it wants.

If your code relies on the order of the keys - you need to stop it from now (by using, say, arrays).

zerkms
  • 230,357
  • 57
  • 408
  • 498