iterating through a javascript object in order

javascript

Solution

Off the top of my head, the only way to do this is get the keys (using `Object.keys(myObj)`), then sort them (using `Array.sort`) and go through it that way. The keys are unordered in the actual object.

So it's something like:

    var keys = Object.keys(myObj);
    keys.sort();
    for(var i=0; i<keys.length; ++i){
        //do something with myObj[keys[i]]
    }

Problem

Here's the situation. I have a JavaScript object and I need to iterate through it, but the order is very important to maintain. I know that JavaScript object is supposed to be unordered, but I was hoping there was some JavaScript equivalent to the following: http://docs.python.org/2/library/collections.html#collections.OrderedDict This is the original ordering ``` { 082013: {'A': 1, 'B', 3} 092013: {'A': 2, 'B', 4} 102013: {'A': 8, 'B', 2} 112013: {'A': 92, 'B', 67} 122013: {'A': 64, 'B', 32} } ``` Then I iterate through it: ``` aArray = []; bArray = []; $.each(data, function(key, value) { aArray.push(value.A); bArray.push(value.B); }); ``` the result (on chrome in particular) seems to render more like this: ``` aArray = [8, 92, 64, 1, 2]; bArray = [2, 67, 32, 3, 4]; ``` This implies that it does not iterate in order, or goes through it in the order of smallest to largest (102013, 112013, 122013, 092013, 082013) So how could I iterate this object in order, if I can at all?

Original source

Related problems