Created
July 12, 2018 06:21
-
-
Save zhaopengme/6529b367e577376e55a152f5b1170a94 to your computer and use it in GitHub Desktop.
array 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
Array.prototype.groupBy = function(prop) { | |
return this.reduce(function(groups, item) { | |
const val = item[prop] | |
groups[val] = groups[val] || [] | |
groups[val].push(item) | |
return groups | |
}, {}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment