Created
May 27, 2019 07:01
-
-
Save paulund/97241ae8bf19049f1b1dfe300df204b2 to your computer and use it in GitHub Desktop.
JavaScript GroupBy function
This file contains hidden or 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 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