-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
_.union doesn't work with arrays of objects #2311
Comments
I'm surprised it doesn't already accept the comparison function. 👍 |
It's worth pointing out that this applies to all the other array computation functions like difference, intersection, unique, etc. Would be nice if the full suite of array functions could be updated to allow use of a different equality comparator for objects. |
Wouldn't it be easier if we would have an option that compares equality based on boolean parameter value that could be passed to _.union() function? If it is true then it would automatically compare all objects in that array. E.g. |
@amiral84 No. That is unrelated. If you want that behaviour, compose union with flatten. |
@michaelficarra Then did I miss the point of this topic? :D |
@amiral84 It appears so. The feature request is explained fully and succinctly in the first comment. |
the underlying problem seems to be in _.union = restArgs(function(arrays) {
return _.uniq(flatten(arrays, true, true));
}); |
This thread inspired me to add var array = [ { 'a': 1, 'b': 2 }, { 'a': 1, 'b': 3 }, { 'a': 1, 'b': 2 } ];
_.uniqWith(array, _.isEqual);
// => [{ 'a': 1, 'b': 2 }, { 'a': 1, 'b': 3 }] |
or something along the lines of |
Dynamic switching sounds like a bad idea. JS uses |
Thanks for this @jdalton, the _opWith functions you mention are absolutely perfect for what I'm trying to achieve. Any idea when they'll be available via release? |
@jdalton good point on the comparisons, but wouldn't you usually use a key for unique on a collection rather than forcing Underscore to detect the entire difference between the objects? Wouldn't the following solve @wilhen01 's request (albeit more verbose than desired) _.chain([{ a: 1 }]).union( [{a: 1}]).unique('a').value();
//=> [{a: 1}] |
|
right, thats my point, the above code works currently as posted. |
@dperrymorrow Think just a tiny bit outside that example and add another property. |
ok, gotcha, sorry... Im not trying to be belligerent, just wanted to totally understand the problem. Id love to submit a pull request on the |
No worries, that would be rad. |
this allows for using custom matcher function for comparison defaults to _.isEqual jashkenas#2311
Wouldn't it be a nicer API to just allow the comparison function to be optionally passed as the final argument, instead of minting four new functions? |
Yes, it could be done but there's complications because methods like |
Right, a totally unfortunate design issue. But making new functions just to allow a comparator doesn't feel like the right solution either. |
ok so additional comparison function parameters are the way to go here then? the only tricky part I forsee is making the parameter parsing a little more hairy as @jdalton mentioned above. _.uniq = _.unique = function(array, isSorted, iteratee, context) {
if (!_.isBoolean(isSorted)) {
context = iteratee;
iteratee = isSorted;
isSorted = false;
}
//... Perhaps adding an additional check to see if isSorted |
It may be the best option for a bad situation. I've gone on a kick of splitting out overloaded functionality recently and have been pretty happy with the result. Though it increases the API surface it allows for simpler implementations and grouping of similarly themed methods like |
have updated this pull request #2368 thanks. |
I'm 👍 for |
👍 @megawac, |
Fwiw lodash will be using |
Yeah, on second thought |
should I pull request on Lodash with the separate method then? |
No need, they're already in lodash's edge master branch.
Not yet. Lodash v4 proofs out some of the ideas of the merge though. |
@jdalton Can you please elaborate a liitle more on implementation of |
@pavnii |
@jdalton That helps me. |
_.union will always produce duplicates when passed arrays of objects.
e.g.
_.union( [ { a:1 } ], [ { a:1 } ])
will return[ { a:1 }, { a:1 } ]
Perversely, underscore's own isEqual function will tell you that the objects in question are equal. Maybe we could have a flag/option which dictates the equality comparison to use, or the option to pass in a comparator?
The text was updated successfully, but these errors were encountered: