Add a function for the symmetric difference of two arrays#309
Conversation
|
Out of curiosity, do you have any real-world code that makes use of this function? |
|
... apparently not. |
|
Can we re-open this? Real-world example (names changed to keep it simple though): I have an array of objects that contain metadata of widget A. I have an array of objects that contain metadata of widget B. I want to know which metadata objects are different between the two. |
|
I should add that the commit from eikes only works with two arrays. I'd like it to work with an arbitrary number of arrays. |
_.symmetricDifference = function() {
return _.reduce(arguments, function(first, second) {
return _.union(_.difference(first, second), _.difference(second, first));
});
} |
…g _.difference accept any number of arguments.
|
Fixed in the above commit. While I think Michael's implementation is an exceptionally beautiful bit of code, if anyone feels like throwing together a faster implementation that's equally small, I'd be willing to take that patch. |
|
Yeah, I definitely don't like my implementation, but I figured I would adhere to the current convention of using other underscore functions in the definition of underscore functions. It'd probably be much more efficient if we removed all those function calls. |
|
Neat! |
|
Woohoo! Thanks! |
There was a previous pull request here, which explained why difference is not symmetric. The functionality hasn't changed though...
#277