I'm doing groupBy function which will accept a callback and return an object with grouped values. If no callback is passed, the method must group by array values.
I've done it, but eslint does't accept it, any suggestions, how to refactor it?
Array.prototype.groupBy = function groupBy(callback) {
if (!callback) {
return this.reduce((acc, x) =>
(acc[(x)] ? acc[(x)].push(x) : acc[(x)] = [x], acc), {});
}
return this.reduce((acc, x) => {
const key = callback(x);
if (!acc[key]) {
return {
...acc,
[key]: [x],
};
}
return {
...acc,
[key]: [...acc[key], x],
};
}, {});
};
}
And eslint errors are :
- 3:26 Arrow function should not return assignment. eslint(no-return-assign)
- 4:54 Unexpected use of comma operator. eslint(no-sequences)