Skip to content
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

Extend _.groupBy to support multi-key objects #216

Open
rianwouters opened this issue Aug 24, 2016 · 0 comments
Open

Extend _.groupBy to support multi-key objects #216

rianwouters opened this issue Aug 24, 2016 · 0 comments
Labels
after modules This should be postponed until after modularization (temporary label, see #220) enhancement

Comments

@rianwouters
Copy link

The current groupBy function only adds each Collection object to the returned hash only once.
In some case one might want to add the same object in multiple groups.

A concrete example is an object having an array property.

Basically this means replacing:

.groupBy = group(function(result, value, key) {
if (
.has(result, key)) result[key].push(value); else result[key] = [value];
});

by

function addToGroup(result, value, key) {
if _.isArray(key) { .each(key, function(k) { addToGroup(result, value, k); }); return; }
if (
.has(result, key)) result[key].push(value); else result[key] = [value];
});

_.groupBy = group(addToGroup);

I implemented this for my backbone collection as groupByMulti but that is rather cumbersome as I don't have access to the underscore internals, in particular the group function.

@jgonggrijp jgonggrijp added after modules This should be postponed until after modularization (temporary label, see #220) enhancement labels Aug 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
after modules This should be postponed until after modularization (temporary label, see #220) enhancement
Projects
None yet
Development

No branches or pull requests

2 participants