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 }
// ]
@Gwunhar
Copy link

Gwunhar commented Mar 22, 2019

I believe your commented result is from a different iteration of the sample code? You're showing more fields in the result than in the pick command in the code.

@ganorberg
Copy link

@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.

@ancyrweb
Copy link

@ganorberg I think he refers to the pick("id", "name", "score) while in the end result there's also age and active

@kropamk
Copy link

kropamk commented Mar 24, 2019

@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

@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