I have an array:
var maxSpeed = {car:300, bike:60, motorbike:200, airplane:1000, helicopter:400, rocket:8*60*60}
I want to sort it and convert to FLAT object that will look exactly the same as the array above. Unfortunately after sorting and creating object I get multi-dimensional object:
var sortable = [];
for (var vehicle in maxSpeed)
sortable.push([vehicle, maxSpeed[vehicle]])
sortable.sort(function(a, b) {return a[1] - b[1]});
function toObject(sortable) {
var rv = {};
for (var i = 0; i < sortable.length; ++i)
rv[i] = sortable[i];
return rv;
}
Check console.logs, one is nested the other one is not. I think I have to modify rv[i] = sortable[i] line, but I'm not sure how?
That's what I get now:
Object
0: Array[2]
1: Array[2]
2: Array[2]
3: Array[2]
4: Array[2]
5: Array[2]
__proto__: Object
And what I want to get:
Object
airplane: 1000
bike: 60
car: 300
helicopter: 400
motorbike: 200
rocket: 28800
__proto__: Object
JSFiddle: