Created
January 12, 2017 15:27
-
-
Save srph/f534c332f80073dd1899f723c1f0f02c to your computer and use it in GitHub Desktop.
JS: Group By
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
| module.exports = function groupBy(arr, fn) { | |
| var group = {}; | |
| arr.forEach(function(v, k) { | |
| var prop = group[fn(v, k)]; | |
| prop == null ? prop = [v] : prop.push(v); | |
| }); | |
| return group; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment