Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Last active August 15, 2018 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreasvirkus/e49d91fbd4b6aef4801319f583ddf629 to your computer and use it in GitHub Desktop.
Save andreasvirkus/e49d91fbd4b6aef4801319f583ddf629 to your computer and use it in GitHub Desktop.
Reverse an object's (sorted!) order by mapping it into an array where the object's key is included within the new constructed object.
const reverseObjIntoArray = obj => Object.keys(obj).sort().reverse().map(key=> ({ key, ...obj[key] }))
reverseObjIntoArray({
124: {
a: 'foo',
b: 'bar'
},
119: {
a: 'foo',
b: 'bart'
},
127: {}
})
/**
// Logs:
[{
key: 127
},
{
key: 124,
a: 'foo',
b: 'bar'
},
{
key: 119,
a: 'foo',
b: 'bart'
}]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment