Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

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

This comment has been minimized.

Copy link

ganorberg commented Mar 24, 2019

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

@rewieer

This comment has been minimized.

Copy link

rewieer commented Mar 24, 2019

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

@kropamk

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Owner 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
You can’t perform that action at this time.