Skip to content

Instantly share code, notes, and snippets.

@evaporei
Created May 28, 2019 23:16
Show Gist options
  • Save evaporei/b6635ec23077858269978cf2637ca14f to your computer and use it in GitHub Desktop.
Save evaporei/b6635ec23077858269978cf2637ca14f to your computer and use it in GitHub Desktop.
Group by without Ramda
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