I have an object literal, where the values of its key are more objects, and one of the keys of the inner objects is named “rank” – and has an floating point value. I want to convert the object literal to an array of the inner objects, sorted by the value of “rank”.
Input Object:
{
452:{
bla:123,
dff:233,
rank:2
},
234:{
bla:123,
dff:233,
rank:1
}
}
Output Array:
[
{ bla:123, dff:233, rank:1},
{ bla:123, dff:233, rank:2 }
]
Example:
Javascript:
That would first
sortthe inner objects by the value ofobj.rankand after thatmapthe containing objects into an Array.Result:
[{rank: 2}, {rank: 5}, {rank: 8}]Reference:
Object.keys,Array.prototype.sort,Array.prototype.mapThe above code contains ECMAscript 262 edition 5 code, which is available in all modern browsers. If you want to support legacy browsers as well, you need to include one of the various ES5-Shim libraries.