Tag Archives: Javascript

Recently found very interesting and sad issue when processing JSON results

assume you have following associative array

var a = {{"5":"five"},{"4":"four"},{"3":"three"},{"2":"two"},{"1":"one"}}

now

for(var n in a){
document.write(i + " = " + a[i] + "<br/>") ;
}

Result in Firefox:

5 = five
4 = four
3 = three
2 = two
1 = one

looks correct

now run in Chrome
Result

1 = one
2 = two
3 = three
4 = four
5 = five

Seem like Chrome autoconverted our associative array to numeric

just keep in in mind when processing such arrays. in my scenario it was when i was processing database results returned in jQuery.ajax and result was JSON associative array with IDs as keys. i have had to discard associative arrays and have regular.