Skip to content

Instantly share code, notes, and snippets.

@paulund
Created May 27, 2019
Embed
What would you like to do?
JavaScript GroupBy function
const groupBy = function (data, key) {
return data.reduce(function (carry, el) {
var group = el[key];
if (group === null) {
group = 'General'
}
if (carry[group] === undefined) {
carry[group] = []
}
carry[group].push(el)
return carry
}, {})
}
export {
groupBy
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment