-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider groupBy
generic operation
#3
Comments
Good one! Will give it a little more thought and search if there's an already existing proposal. My only concern with it is that it returns an |
A example supporting both Objects and Maps Array.prototype.groupBy = function (f, init = {}) {
let rfMap = (acc, el) => {
let id = f(el);
let els = [... acc.get(id) || [], el];
return acc.set(id, els);
}
let rfObject = (acc, el) => {
let id = f(el);
let els = [... (acc[id] || []), el];
acc[id] = els;
return acc;
}
return this.reduce(init instanceof Map ? rfMap : rfObject , init)
}
let {true: t} = [1,2,3].groupBy(x => x == 2)
console.log(t , [1,2,3].groupBy(x => x == 2, new Map())) |
groupBy is great but this
is much nicer than
|
const { even, odd } = Object.groupBy([1, 2, 3], x => x % 2 ? 'odd' : 'even') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's a bit more generic operation "partition"
Can be used as
partition
as:Maybe should we accumulate over an actual
Map
allowingf
return any object (?!)The text was updated successfully, but these errors were encountered: