Skip to content

Instantly share code, notes, and snippets.

@hG3n
Created November 22, 2018 13:08
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 hG3n/afba8863a1f107f6188fe0eb7bcd3a79 to your computer and use it in GitHub Desktop.
Save hG3n/afba8863a1f107f6188fe0eb7bcd3a79 to your computer and use it in GitHub Desktop.
Group Elements by several matching attributes
function groupBy(array: any[], f): any {
const groups = {};
array.forEach(function (o) {
const group = JSON.stringify(f(o));
groups[group] = groups[group] || [];
groups[group].push(o);
});
return Object.keys(groups).map(function (group) {
return groups[group];
});
}
var result = this.groupBy(coordinates, function (item) {
return [item.x, item.y];
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment