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

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

本文关键字:Javascript 函数 参数 比较 何过滤 过滤 数组      更新时间:2023-09-26

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

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]

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