Created
September 9, 2018 06:12
-
-
Save suissa/11d2a0e26e3d3da5230c131969a51edd to your computer and use it in GitHub Desktop.
JavaScript - groupby - mais semantico eh nois q voa bruxaooooo
This file contains 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 group = (obj, field) => obj[field] | |
const addValue = (result, obj, field) => | |
result[obj[field]] || [] | |
const addToGroup = (result, obj, field) => | |
[...(addValue(result, obj, field)), obj] | |
const toNewGroupUsing = (field) => (result, obj) => ({ | |
...result, | |
[group(obj, field)]: addToGroup(result, obj, field) | |
}) | |
const groupThis = (list) => (field) => | |
list.reduce(toNewGroupUsing(field), {}) | |
const groupBy = groupThis(list) | |
console.log( | |
groupBy("level") | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment