Last active
August 15, 2018 09:06
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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