Created
February 14, 2020 16:48
-
-
Save BrunoGiubilei/48428d8545a34cc59f06e8d35873b953 to your computer and use it in GitHub Desktop.
Group By prototype for array of object
This file contains 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() { | |
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