过滤器函数的 JavaScript 函数参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11235863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:29:57  来源:igfitidea点击:

JavaScript function arguments for filter function

javascriptfilter

提问by Kevin

numbers = [1,2,3,4,5,4,3,2,1]; 
var filterResult = numbers.filter(function(i){
    return (i > 2);
});       

I don't understand how this works. if I omit the i as a function argument it breaks the function but the i isn't tied to anything so why does it need to be there?

我不明白这是如何工作的。如果我省略 i 作为函数参数,它会破坏函数,但 i 不与任何东西相关,所以为什么需要在那里?

回答by Hamish

.filter(Array.prototype.filter) calls the supplied function with 3 arguments:

.filter( Array.prototype.filter) 使用 3 个参数调用提供的函数:

function(element, index, array) {
    ...
  • elementis the particular array element for the call.
  • indexis the current index of the element
  • arrayis the array being filtered.
  • element是调用的特定数组元素。
  • index是元素的当前索引
  • array是被过滤的数组。

You can use any or all of the arguments.

您可以使用任何或所有参数。

In your case, irefers to the elementand is used in the body of your function:

在您的情况下,i指的element是在您的函数体中使用的和:

function(i){
    return (i > 2);
}

In other words, "filter elements where elementis greater than 2".

换句话说,“过滤元素element大于 2”

回答by Travis J

i is a reference to the current object in the set when inside that closure. It could be named anything as it is just a variable, but then would have to have the same name inside the closure. Instead of using function(){}you could use a callback which is how filterwas designed.

i 是在该闭包内时对集合中当前对象的引用。它可以命名为任何名称,因为它只是一个变量,但是在闭包中必须具有相同的名称。function(){}您可以使用回调,而不是使用它的filter设计方式。

The reference is done implicitly by the definition of .filter, you can read more here: http://msdn.microsoft.com/en-us/library/ff679973(v=vs.94).aspx

该参考由 的定义隐式完成.filter,您可以在此处阅读更多信息:http: //msdn.microsoft.com/en-us/library/ff679973(v=vs.94).aspx

回答by Jon

That iis the formal parameterfor the function you are supplying to .filter(). If you do not insert it, the function will not have any way1 to refer to the argument it's being passed (the iinside the function body will then refer to some other entity that might not even be defined -- window.iwould be typical).

i是您提供给的函数的形式参数.filter()。如果你不插入它,函数将没有任何方法来引用它正在传递的参数(i函数体内部将引用一些甚至可能没有定义的其他实体——window.i这将是典型的)。



1 that is technically a lie, but consider it true for the purposes of this discussion

1 这在技术上是谎言,但为了本次讨论的目的考虑它是真的

回答by tskuzzy

The iis actually very important. It tells gives the filter function information about the elements it's acting on. In fact it's used right here (i > 2).

i实际上是非常重要的。它告诉过滤器函数有关它正在作用的元素的信息。事实上,它就在这里使用(i > 2)

This keeps elements whose value is greater than 2.

这会保留值大于 2 的元素。

回答by dwilbank

An old thread indeed, but just filling in what remains unsaid.

确实是一个旧线程,但只是填补了未说的内容。

The parentheses are there for you the programmer to insert whatever variable name makes sense for your specific program.

括号可供程序员插入任何对您的特定程序有意义的变量名称。

If you choose 'i', most other (beginner) programmers might think 'Oh, i means index'. Which would be wrong.

如果您选择“i”,大多数其他(初学者)程序员可能会认为“哦,我是指索引”。这是错误的。

If you use one argument instead of three, I'd choose 'el' to represent the element, or if your array contains flavors of soda, I'd choose 'flavor'.

如果您使用一个参数而不是三个参数,我会选择 'el' 来表示元素,或者如果您的数组包含苏打水的口味,我会选择 'flavor'。

回答by German

That's ES5 notation and maybe if you see it in ES6 notation you would understand why the "i" is a must:

那是 ES5 表示法,也许如果您在 ES6 表示法中看到它,您就会明白为什么“i”是必须的:

numbers.filter(i => i > 2);

A variable must always be used to refer to the item of the array that you process in each iteration (in this case "i"). It has to be passed as argument to the entry point of the function (in ES6 that goes before the arrow).

必须始终使用变量来引用您在每次迭代中处理的数组项(在本例中为“i”)。它必须作为参数传递给函数的入口点(在 ES6 中,它位于箭头之前)。