Created
November 22, 2018 13:08
-
-
Save hG3n/afba8863a1f107f6188fe0eb7bcd3a79 to your computer and use it in GitHub Desktop.
Group Elements by several matching attributes
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
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