简体   繁体   English

将参数传递给filter()

[英]Passing arguments to filter()

I have a function that takes an array and then a number of arguments. 我有一个函数,它需要一个数组,然后有多个参数。 In that function I want to use the filter() method on the array, but I want to use arguments[i]. 在该函数中,我想在数组上使用filter()方法,但我想使用arguments [i]。 As an example: 举个例子:

function destroyer(arr) {
  var test = arr.filter(function(value){
    return value != arguments[1];
  });
  return test;
}

console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));

Calling arguments[1] there doesn't give me what I want, because I'm guessing the anonymous function has its own arguments that filter() is using. 调用arguments [1]并没有给我我想要的东西,因为我猜测匿名函数有自己的filter()使用的参数。 One solution I thought of was creating an array and copying the arguments to that. 我想到的一个解决方案是创建一个数组并将参数复制到该数组。 But I was wondering if there was a way to pass the arguments to filter? 但是我想知道是否有一种方法可以将参数传递给过滤器? I know filter has a thisArg value, but I'm not sure how to use that to get what I want. 我知道过滤器具有thisArg值,但是我不确定如何使用该值来获得所需的值。

Edit: I have tried googling for a solution, and most of the solutions involve using bind or apply. 编辑:我已经尝试过使用Google搜索解决方案,并且大多数解决方案都涉及使用bind或apply。 However, I couldn't find anything directly related to filter() or similar methods like forEach, reduce, map, etc. I've tried using "this" as the thisArg value, but that doesn't seem to do anything. 但是,我找不到与filter()或诸如forEach,reduce,map等类似方法直接相关的任何内容。我尝试使用“ this”作为thisArg值,但似乎没有任何作用。 I've tried using bind and apply with it, but I can't seem to get the correct syntax for it. 我尝试使用bind并对其应用,但是我似乎无法为其获取正确的语法。

If you have access to an ES6 environment, you can use "spread parameters" ( ... ) to represent your variable-length argument list. 如果可以访问ES6环境,则可以使用“扩展参数”( ... )表示可变长度参数列表。 Along with arrow functions, and Array#includes , your code could be quite compact: 连同箭头功能和Array#includes ,您的代码可能非常紧凑:

function destroyer(arr, ...destroy) {
  return arr.filter(value => !destroy.includes(value));
}

I have a working solution (with copying the arguments to an array), but I'm more interested in trying to figure how to pass the arguments to filter() to better understand how filter() and other similar methods work. 我有一个可行的解决方案(将参数复制到数组中),但是我对尝试弄清楚如何将参数传递给filter()以更好地了解filter()和其他类似方法的工作方式更加感兴趣。

You don't need to pass the arguments to filter() . 您不需要将参数传递给filter() The arguments can simply be made available in the scope of the filter callback. 可以简单地使参数在过滤器回调的范围内可用。 However, arguments is special; 但是, arguments很特殊。 it refers to the arguments of the current function. 它引用当前函数的参数。 There is no way to refer to the arguments of some other function, such as the outer function. 无法引用其他函数(例如外部函数)的参数。 Therefore, there is no alternative but to save the arguments under a different variable name (such as args ) in the outer function; 因此,别无选择,只能参数保存在外部函数中的其他变量名称下(例如args )。 then you can refer to that variable from within the callback, because the callback "closes" over that variable. 那么您可以从回调中引用该变量,因为回调“关闭”了该变量。

If you're really intent on "passing" the list of numbers to be destroyed to the inner function, instead of just letting it access them by referring to a variable created in the outer scope, then yes, in theory you could use thisArg : 如果您确实打算将要销毁的数字列表“传递”给内部函数,而不是仅仅通过引用在外部作用域中创建的变量让它访问它们,那么可以,理论上,您可以使用thisArg

function destroyer(arr/*, number to destroy*/) {
  return arr.filter(do_not_destroy, arguments);
                                    ^^^^^^^^^ PASS thisArg
}

Now we can define do_not_destroy as 现在我们可以将do_not_destroy定义为

function do_not_destroy(elt) {
  return [].slice.call(this, 1).indexOf(elt) === -1;
                       ^^^^ REFER TO thisArg (arguments)
}

Note two things here. 在这里注意两件事。 First, we refer to the list of arguments as this , because we passed them to filter using thisArg . 首先,我们将参数列表称为this ,因为我们使用thisArg将它们传递给了filter Second, we cannot write this.slice... , and instead must write [].slice.call(this... , because this is an arguments object, which is not a "real" array and does not have the slice method. 其次,我们不能编写this.slice... ,而必须编写[].slice.call(this... ,因为this是一个参数对象,它不是“真实”数组,并且没有slice方法。

To make do_not_destroy independent of the fact that its parameter is a special arguments object, with an extra parameter (the array) at the beginning, we could do the conversion to an array within the destroyer function: 为了使do_not_destroy的参数是一个特殊的自变量对象,并在开头添加一个额外的参数(数组),这一点是独立的,我们可以在destroyer函数中转换为数组:

function destroyer(arr/*, number to destroy*/) {
  return arr.filter(do_not_destroy, [].slice.call(arguments, 1));
}

Now we can define do_not_destroy as 现在我们可以将do_not_destroy定义为

function do_not_destroy(elt) {
  return this.indexOf(elt) === -1;
}

But now we are basically back to where we started. 但是现在我们基本上回到了起点。 Instead of "passing" the list of values to be omitted by merely letting the inner function (callback) refer to a variable set in the outer scope, we are passing them to the callback using the back-door thisArg mechanism. 我们不是通过仅让内部函数(回调)引用外部范围中的变量集来“传递”要省略的值列表,而是使用后门thisArg机制将它们传递给回调。 I don't really see the point in this. 我真的不明白这一点。

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

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