0

I have the following array:

['aaa', 'bbb', 'ccc', 'ddd']

My goal is to remove from it unexpected values:

I tried to do it with underscore without function like below:

_.without(['aaa', 'bbb', 'ccc', 'ddd'], 'bbb', 'ccc');

It works fine, but unfortunately it does not work with array:

_.without(['aaa', 'bbb', 'ccc', 'ddd'], ['bbb', 'ccc']);

I've googled a bit and find the post underscore.js - Is there a function that produces an array thats the difference of two arrays?

But in my case this one also does not work, namely it returns somthing like that:

"a","a","a"

when I tired to use apply function.

Can some one suggest what need to be done to remove all unexpected keys with array?

2 Answers 2

4

Have you tried _.difference?

_.difference(['aaa', 'bbb', 'ccc', 'ddd'], ['bbb', 'ccc']);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answer. Accepted. Before,I've briefly tried the following function. But actually I was a bit surprised why the following example _.difference([1, 2, 3, 4, 5], [5, 2, 10]); skip 10. But now I see that it is described in the documentation.
1

For the sake of completeness, that's how it can be done with _.without:

var source = ['aaa', 'bbb', 'ccc', 'ddd'];
var blacklist = ['bbb', 'ddd'];
var without = _.without.apply(_, [source].concat(blacklist));

Comments

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.