12

I was expecting the lodash without function to take an array of values, but instead takes multiple args. How do I pass it an array and make it work.

Example:

var bar = {
    foo: ['a', 'b', 'c']
};


_.without(bar.foo, 'c', 'a'); // works;
_.without(bar.foo, ['c', 'a']); // doesn't work

My exclusion list has to be passed in as an array or variable so it would be useful to know how to use an array with the without function.

4 Answers 4

13

In this case, you can use the array of values as it is, with _.difference, like this

console.log(_.difference(bar.foo, ['a', 'c']));
[ 'b' ]
Sign up to request clarification or add additional context in comments.

1 Comment

this is probably the better way to do what I need, but I accepeted Alexander's answer since it was specific to the exact question. However, I'll be using your solution in my actual project. Thanks!
13

If you're in an environment where you can use the ES6 spread operator, ..., then

.without(bar.foo, ...['c', 'a']);

Comments

4

You can use .apply

var bar = {
    foo: ['a', 'b', 'c']
};


console.log(_.without.apply(_, [bar.foo].concat(['c', 'a'])));
<script src="https://rawgit.com/lodash/lodash/3.0.1/lodash.min.js"></script>

Comments

1

Could use spread operator

var bar = {
    foo: ['a', 'b', 'c']
};

var arr = ['c', 'a'];

console.log(_.without([bar.foo], ...arr) ;

1 Comment

straight copy of a previous answer from 2 years ago: -1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.