Once you have the array, you can reconstruct the objects from the array in any order you like, achieving exactly what you want to do. This works in all browsers I'm aware of, but it depends on implementation quirks and may break at any time. You should never make assumptions about the order of elements in a JavaScript object.
let objSorted = {}
sortable.forEach(function(item){
objSorted[item[0]]=item[1]
})
In ES8, you can use Object.entries() to convert an object to an array:
We don't want to copy the entire data structure, or use an array where an associative array is required.
Here's another way to do the same thing as bonna:
Move them into an array, sort the array, and use that array for your purposes. The solution is as follows:
Once you have the array, you can reconstruct the objects from the array in any order you like, achieving exactly what you want to do. This works in all browsers I'm aware of, but it depends on implementation quirks and may break at any time. You should never make assumptions about the order of elements in a JavaScript object.
In ES8, you can use
Object.entries()
to convert an object to an array: