underscore icon indicating copy to clipboard operation
underscore copied to clipboard

_.without to accept array as second argument

Open ondrejhlavacek opened this issue 15 years ago • 10 comments

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]]

ondrejhlavacek avatar Jan 21 '11 12:01 ondrejhlavacek

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])

jashkenas avatar Jan 21 '11 12:01 jashkenas

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).

ondrejhlavacek avatar Jan 21 '11 12:01 ondrejhlavacek

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.

fbuchinger avatar Feb 04 '11 15:02 fbuchinger

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.

rubenstolk avatar Sep 22 '12 08:09 rubenstolk

@rubenstolk's answer works except he didn't comment his code out so the underscores interpreted as italic. For future generations! :railway_car: :rocket: :rainbow: :raised_hands:

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

reggi avatar Dec 17 '14 19:12 reggi

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

rubenstolk avatar Dec 18 '14 03:12 rubenstolk

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

addisonbgross avatar Apr 15 '15 22:04 addisonbgross

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

agwidarsito avatar Dec 27 '15 12:12 agwidarsito

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]

rally25rs avatar Feb 01 '17 19:02 rally25rs

Thank you @addisonbgross!

PixelTom avatar Jan 04 '19 00:01 PixelTom