| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 1 7 7 7 7 1 6 7 39 39 19 20 39 7 | function groupBy(xs, iteratee, thisArg) {
let i = 0;
let groups = {};
let fn;
if (typeof iteratee === 'string') {
fn = x => x[iteratee];
} else {
fn = thisArg ? iteratee.bind(thisArg) : iteratee;
}
for (let x of xs) {
let key = fn(x, i, xs);
if (groups[key]) {
groups[key].push(x);
} else {
groups[key] = [x];
}
i += 1;
}
return groups;
}
export default groupBy;
|