Last active
March 25, 2019 10:50
-
-
Save tomi/4cc5d42b55746eb33df58be1386e977a to your computer and use it in GitHub Desktop.
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
import { from } from "fromfrom"; | |
const users = [ | |
{ id: 1, name: "John", age: 31, score: 2244, active: true }, | |
{ id: 2, name: "Jane", age: 32, score: 2492, active: false }, | |
{ id: 3, name: "Luke", age: 33, score: 2500, active: false }, | |
… | |
{ id: 1000, name: "Mary", age: 34, score: 2290, active: true }, | |
]; | |
from(users) | |
.filter(user => user.active) | |
.pick("id", "name", "score") | |
.sortByDescending(user => user.score) | |
.take(5) | |
.toArray(); | |
// End result: | |
// [ | |
// { id: 214, name: "May", score: 5622 }, | |
// { id: 353, name: "Peter", score: 5212 }, | |
// { id: 32, name: "Macy", score: 5112 }, | |
// { id: 972, name: "Laura", score: 5023 }, | |
// { id: 429, name: "Bob", score: 4900 } | |
// ] |
@ganorberg I think he refers to the pick("id", "name", "score)
while in the end result there's also age
and active
@ganorberg I think it's not about "..." just about method pick(), which takes only three fields but in result we see 5 fields in one object
there's a score.
instead of score:
on line 8 please fix, it's triggering my OCD :D definitely gonna use this
Thank you for your comments! The previous version was indeed erroneous. The end result objects should only contain properties id
, name
and score
. It should be now fixed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Gwunhar There is a shorthand in the users array that suggests 1,000 objects. See the "..." on line 7 and the numbering of ids from 1, 2, 3 to 1,000.