Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zhaopengme/6529b367e577376e55a152f5b1170a94 to your computer and use it in GitHub Desktop.
Save zhaopengme/6529b367e577376e55a152f5b1170a94 to your computer and use it in GitHub Desktop.
array group by
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