Skip to content

Instantly share code, notes, and snippets.

@tomi
Last active March 25, 2019 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomi/4cc5d42b55746eb33df58be1386e977a to your computer and use it in GitHub Desktop.
Save tomi/4cc5d42b55746eb33df58be1386e977a to your computer and use it in GitHub Desktop.
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 }
// ]
@cyberhck
Copy link

cyberhck commented Mar 25, 2019

there's a score. instead of score: on line 8 please fix, it's triggering my OCD :D definitely gonna use this

@tomi
Copy link
Author

tomi commented Mar 25, 2019

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