问题
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?
回答1:
Have you tried _.difference?
_.difference(['aaa', 'bbb', 'ccc', 'ddd'], ['bbb', 'ccc']);
回答2:
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));
来源:https://stackoverflow.com/questions/32851118/underscore-js-with-two-arrays