Skip to content

Instantly share code, notes, and snippets.

@suissa
Last active August 29, 2018 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suissa/2a40290416f306b8adbbec7b0414decc to your computer and use it in GitHub Desktop.
Save suissa/2a40290416f306b8adbbec7b0414decc to your computer and use it in GitHub Desktop.
JavaScript - groupby - filter 06
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