Last active
August 29, 2018 21:31
-
-
Save suissa/2a40290416f306b8adbbec7b0414decc to your computer and use it in GitHub Desktop.
JavaScript - groupby - filter 06
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[obj[field]] = (!result[obj[field]]) | |
? [obj] | |
: [...result[obj[field]], obj] | |
return result | |
} | |
const groups = list.reduce(toNewGroup("level"), {}) | |
console.log(groups) | |
/** | |
{ senior: | |
[ { 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] } ], | |
pleno: | |
[ { 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