I need to convert an object map (server response) to an array and order this by the object's key.
Given:
var ppl = {
1: { name: 'Fred', age: 31 },
0: { name: 'Alice', age: 33 },
3: { name: 'Frank', age: 34 },
2: { name: 'Mary', age: 36 }
}
console.log(ppl);
It appears that the object when created is sorted by the key, the console shows this:
{
0: { name: 'Alice', age: 33 }
1: { name: 'Fred', age: 31 }
2: { name: 'Mary', age: 36 }
3: { name: 'Frank', age: 34 }
}
Then I use lodash to convert to an array like this:
var arr = _.toArray(ppl);
console.log(arr)
- I don't think the order of the initial object map is guaranteed, is that correct?
- How can I ensure the array order is based on the object map's keys?