devbox-linq
Version:
Lambda style operations using arrow functions
22 lines (17 loc) • 474 B
JavaScript
Array.prototype.groupBy = function (expression) {
if (!this.length)
return this;
var group = [];
for (let i = 0; i < this.length; i++) {
let result = expression(this[i]),
ref;
if (ref = group.firstOrDefault(x => x.key === result)) {
ref.add(this[i]);
continue;
}
let arr = [this[i]];
arr.key = result;
group.add(arr);
}
return group;
};