简体   繁体   English

具有两个数组的underscore.js

[英]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功能的下划线来做到这一点:

_.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? 我已经用Google搜索了一下,找到了underscore.js帖子-是否有一个函数可以生成一个数组,即两个数组的区别?

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. 当我厌倦了使用Apply功能时。

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 : 为了完整起见,使用_.without可以做到这_.without

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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