繁体   English   中英

如何过滤数组并将其与Javascript中的函数参数进行比较?

[英]How to filter an array and compare it with the function arguments in Javascript?

我已经尝试了下面的代码,但是没有得到满意的结果。

function destroyer(arr) {
// Remove all the values

var newArray = arr.filter(function(x){

if(x == arr[0]){
 return x;
}
});
return newArray;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

最简单的一个:

function without(array, exclude) {
    return array.filter(function(x) { return exclude.indexOf(x) === -1; });
}

您可以这样使用它: without([1,2,3,4,5], [1,2]) // returns [3,4,5]

或者,您可以尝试像这样处理参数列表,但是想法是一样的。

暂无
暂无

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

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