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

_.without to accept array as second argument #104

Closed
ondrejhlavacek opened this issue Jan 21, 2011 · 10 comments
Closed

_.without to accept array as second argument #104

ondrejhlavacek opened this issue Jan 21, 2011 · 10 comments

Comments

@ondrejhlavacek
Copy link

The current definition of _.without accept array as the first argument and then any number of following args. I know it might get tricky, but it would be helpful, if I could pass another array as argument.

The new functionality would work as
_.without([1,2,3], [1, 2])
returns
[3]

As far as I know _.without does not work deep, so it might not be overcomplicated -
_.without([1,2,3,[1]], [1], 1)
returns
[2, 3, [1]]

@jashkenas
Copy link
Owner

Fortunately, JavaScript has a built-in way to pass a function any number of arguments.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/function/apply

_.without.apply(_, [1, 2, 3], [1, 2])

@ondrejhlavacek
Copy link
Author

Sure thing! Stupid me, how could I forget...

It seems that it needs to be called like this

_.without.apply(_, [[1,2,3], 1, 2])

Which makes it just a little bit complicated (unshifting the exlusion array with the original array).

@fbuchinger
Copy link

UPDATE: I created a new ticket for this issue: https://github.com/documentcloud/underscore/issues/issue/118

I have to agree to both of you: to Jeremy for keeping the _.without signature consistent and to Ondrey for finding Jeremys proposed workaround a bit hackish (it doesn't seem to work for me too).

Thus, I want to propose the introduction of two new underscore functions: _.difference(*arrays) and _.symdifference(a,b): difference would find out all elements in from the first array that don't appear in the other supplied arrays, where _.symdifference() would return those elements that are either in a or in b but not in both.

The "inspiration" for these functions comes from the python set datatype and IMHO they shouldn't be just limited to arrays, since they are also applicable to objects.

@rubenstolk
Copy link

Just a quick way of writing it with underscore: .without.apply(, [x].concat(y)), will return array of values in x without the values in y.

@reggi
Copy link

reggi commented Dec 17, 2014

@rubenstolk's answer works except he didn't comment his code out so the underscores interpreted as italic. For future generations! 🚃 🚀 🌈 🙌

_.without.apply(_, [originalArray].concat(removeArray));

@rubenstolk
Copy link

I think Github wasn't doing that 2 years ago @reggi!

@addisonbgross
Copy link

For those who find their way here.... _.difference([0, 1, 3, 9], [1, 3]); // => [0, 9]

From: http://stackoverflow.com/questions/5722254/underscore-js-is-there-a-function-that-produces-an-array-thats-the-difference

@agwidarsito
Copy link

I know this is closed, but ES6's spread operator will help with this ;)

@rally25rs
Copy link

For anyone who doesn't want to search for ES6 spread operator that @agwidarsito mentioned, what he is saying is that you can do this in ES6:

const items = [1,2,3,4];
const itemsToRemove = [3,4,5];
const result = _.without(items, ...itemsToRemove);
// result is [1,2]

@PixelTom
Copy link

PixelTom commented Jan 4, 2019

Thank you @addisonbgross!

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants