Skip to content

Instantly share code, notes, and snippets.

@paulund
Created May 27, 2019 07:01
Show Gist options
  • Save paulund/97241ae8bf19049f1b1dfe300df204b2 to your computer and use it in GitHub Desktop.
Save paulund/97241ae8bf19049f1b1dfe300df204b2 to your computer and use it in GitHub Desktop.
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