Originally I had this issue a while back where while the dataset was correct, relying on ordering in javascript as far as an array is concerned wasn't correct, so my solution was something like this, insofar as the json being returned
var json = { //Returned from a database
data: {
_0: {key1: val1, key2: val2},
_1: {key1: val1, key2: val2},
...etc etc etc
}
};
var new_arr = [];
for(var i = 0; i < Object.keys(json.data).length; i++) {
var obj = json.data["_"+i];
new_arr.push(obj);
}
console.log(new_arr);
In IE8-11, Firefox, Opera (or any other browser) this behaves as you'd expect. The order is preserved per the keys in the original json object returned.
Chrome, however, puts this out of order abritrarily. The array is not in the expected order. For example, in at least one case, "_36" appears before "_0" in the console.log
, then another key is arbitrarily out of order.
Keep in mind the JSON object returns correctly. It's just reordering the data
element of the object isn't being pushed into an array correctly. What am I missing?
Note 1: The key/value pairings inside of _0
et al. do not matter. That is not where the issue is. It is with me running a loop, and the array not being in the right order.
Note 2: The loop is correct. It's accessing the properties of json.data in the right order. The problem is that they aren't going into the array in the right order.