Created
May 28, 2019 23:16
-
-
Save evaporei/b6635ec23077858269978cf2637ca14f to your computer and use it in GitHub Desktop.
Group by without Ramda
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
const data = [ | |
{ | |
category: 'action', | |
value: 'jorgs' | |
}, | |
{ | |
category: 'comedy', | |
value: 'lucians' | |
}, | |
{ | |
category: 'action', | |
value: 'pedrs' | |
}, | |
{ | |
category: 'drama', | |
value: 'lucs' | |
}, | |
] | |
const prop = name => obj => obj[name] | |
const groupBy = grouper => list => list.reduce((acc, curr) => ({ | |
...acc, | |
[grouper(curr)]: [curr, ...(acc[grouper(curr)] || [])], | |
}), {}) | |
groupBy(prop('category'))(data) | |
// { | |
// action: [ | |
// { | |
// category: "action", | |
// value: "jorgs" | |
// }, | |
// { | |
// category: "action", | |
// value: "pedrs" | |
// } | |
// ], | |
// comedy: [ | |
// { | |
// category: "comedy", | |
// value: "lucians" | |
// } | |
// ], | |
// drama: [ | |
// { | |
// category: "drama", | |
// value: "lucs" | |
// } | |
// ] | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment