Last active
August 29, 2018 21:31
-
-
Save suissa/c2066f01b8dd653b608eec46729c3692 to your computer and use it in GitHub Desktop.
JavaScript - groupby - filter 05
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 toNewGroup = (field) => (result, obj) => { | |
result[field] = (!result[field]) | |
? [obj] | |
: [...result[field], obj] | |
return result | |
} | |
const groups = list.reduce(toNewGroup("level"), {}) | |
console.log(groups) | |
/** | |
{ level: | |
[ { id: 1, | |
name: 'Suissa', | |
active: true, | |
level: 'senior', | |
tags: [Array] }, | |
{ id: 2, | |
name: 'Bella', | |
active: true, | |
level: 'senior', | |
tags: [Array] }, | |
{ id: 3, | |
name: 'Joana', | |
level: 'senior', | |
active: true, | |
tags: [Array] }, | |
{ id: 4, | |
name: 'John', | |
level: 'pleno', | |
active: false, | |
tags: [Array] } | |
] | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment