Last active
April 27, 2017 18:20
-
-
Save antoniocsoares/e509ec660733ff9f9821c1bb0d814efe to your computer and use it in GitHub Desktop.
Ordering one object by values (Integer) - ES6
This file contains 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
// example | |
const list = {"you": 100, "me": 75, "foo": 116, "bar": 15}; | |
Object | |
.keys(list) | |
.sort((a, b) => list[a]-list[b]) | |
.reduce((obj, key) => ({ | |
...obj, | |
[key]: list[key] | |
}), {}) | |
// output | |
// {"bar": 15, "foo": 116, "me": 75, "you": 100} | |
// example without spread operator | |
const list = {"you": 100, "me": 75, "foo": 116, "bar": 15}; | |
Object | |
.keys(list) | |
.sort((a, b) => list[a]-list[b]) | |
.reduce((obj, key) => Object.assign(obj, {[key]: list[key]})); | |
// output | |
// {"bar": 15, "foo": 116, "me": 75, "you": 100} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment