简体   繁体   中英

underscore.js with two arrays

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?

您是否尝试过_.difference

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM