Skip to content

Instantly share code, notes, and snippets.

@BrunoGiubilei
Created February 14, 2020 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrunoGiubilei/48428d8545a34cc59f06e8d35873b953 to your computer and use it in GitHub Desktop.
Save BrunoGiubilei/48428d8545a34cc59f06e8d35873b953 to your computer and use it in GitHub Desktop.
Group By prototype for array of object
Array.prototype.groupBy = function() {
var self = this, p = arguments[0];
return self.map(m => m[p.prop])
.reduce((a, d) => {if (!a.includes(d)) { a.push(d); } return a;}, [])
.map(m => self
.filter(f => f[p.prop] === m));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment