[英]Finding A - B from two arrays using underscore.js
I have to filter out certain elements from an array in javascript and thought of using underscore.js for this purpose.我必须从 javascript 中的数组中过滤掉某些元素,并考虑为此目的使用 underscore.js。 As I am new to it,some help is appreciated.Please refer the code below, I have to find A \ B and assign the result to C. Does underscore.js has any convinience method to do that?
由于我是新手,希望能提供一些帮助。请参考下面的代码,我必须找到 A \ B 并将结果分配给 C。underscore.js 是否有任何方便的方法来做到这一点?
function testUnderScore(){
alert("underscore test");
var a = [84, 99, 91, 65, 87, 55, 72, 68, 95, 42];
var b = [ 87, 55, 72,42 ,13];
var c = [];
alert(c);
}
By using difference
method:通过使用
difference
方法:
var c = _.difference(a, b);
http://documentcloud.github.com/underscore/#difference http://documentcloud.github.com/underscore/#difference
I have to find A- B and assign the result to C. Does underscore.js has any convinience method to do that?
我必须找到 A- B 并将结果分配给 C。underscore.js 是否有任何方便的方法来做到这一点?
Yes, you can use difference
[View Docs] method:是的,您可以使用
difference
[View Docs]方法:
var c = _.difference(a, b);
How about:怎么样:
var a = [1, 2, 5, 6], b = [6], c;
c = a.filter(
function (aItem) {
return !(~b.indexOf(aItem));
}
);
console.log(c);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.