underscore.js with two arrays

允我心安 提交于 2019-12-25 03:40:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!