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