简体   繁体   English

我无法理解 Array.prototype.filter() 中的“元素”参数

[英]I'm having trouble understanding the “element” parameter in Array.prototype.filter()

I've been learning JavaScript through Codecademy for the past few months and right now I'm working on a credit card validator exercise.在过去的几个月里,我一直在通过 Codecademy 学习 JavaScript,现在我正在做一个信用卡验证器练习。

My goal in this step is to iterate every other element in an array, and I've used the following code to do so, which works perfectly (provided by dannymac in another post):我在这一步的目标是迭代数组中的所有其他元素,并且我使用了以下代码来执行此操作,效果很好(由dannymac在另一篇文章中提供):

let checkDigits = array.filter((element, index) => {
    return index % 2 === 0
    })
  console.log(checkDigits)

The thing is, even though I understand what is happening, I don't get where the " element " parameter is used.问题是,即使我了解正在发生的事情,我也不知道“元素”参数的使用位置。 I've checked the documentation but since I'm a noob to this I still can't really see it.我已经检查了文档,但是由于我对此很陌生,所以我仍然看不到它。

I know this might look like a stupid question but I'd appreciate if somebody could briefly explain it to me.我知道这可能看起来像一个愚蠢的问题,但如果有人能简要地向我解释一下,我将不胜感激。

Thank you very much.非常感谢。

Kind regards, Vic.亲切的问候,维克。

You are not using element in your case... The filter you have implemented will select even elements regarding their position in the array.您没有在您的情况下使用元素...您已实现的过滤器将 select 甚至与数组中的 position 有关的元素。

Here is an example where you use element:这是使用元素的示例:

const array = [4,12,15,9]
let checkDigits = array.filter((element, index) => {
    return element > 9
    })
  console.log(checkDigits)

/*
[12,15]
*/

In this situation, it's not used - it's just an unused parameter.在这种情况下,它没有被使用——它只是一个未使用的参数。

It's easy to construct this situation without relying on built-ins: just define a function which takes 2 arguments and only uses the second:在不依赖内置函数的情况下很容易构建这种情况:只需定义一个 function ,它需要 2 个 arguments 并且只使用第二个:

 const fn = (arg1, arg2) => { console.log('arg2 is', arg2); }; fn('abc', 'def');

In the case of .filter , its function signature is always the same: the first argument passed to the callback is the element being iterated over, and the second argument passed to the callback is the iteration index.对于.filter ,它的 function 签名始终相同:传递给回调的第一个参数是被迭代的元素,传递给回调的第二个参数是迭代索引。 Not all arguments have to be referenced in a function;并非所有 arguments 都必须在 function 中引用; a function may even define some arguments and then use none of them (though that'd be pretty weird).一个 function 甚至可以定义一些 arguments 然后使用它们(尽管这很奇怪)。 Here, since you can only get a reference to the 2nd argument by also assigning a name to the 1st argument, the element has to be listed in the parameter list.在这里,由于您只能通过为第一个参数分配名称来获得对第二个参数的引用,因此该element必须列在参数列表中。

A common convention for unused parameters is to give it a name of an underscore, eg:未使用参数的常见约定是给它一个下划线的名称,例如:

let checkDigits = array.filter((_, index) => {

This'll work just as well as your current code, but (to some) may look less confusing.这与您当前的代码一样有效,但(对某些人来说)可能看起来不那么令人困惑。

if you have an array like [1, 3, 4, 5, 6, 2, 4] and you run filter on it, it will iterate over each element in the array and execute the callback function you provide.如果您有一个类似[1, 3, 4, 5, 6, 2, 4]的数组并且您对其运行filter ,它将遍历数组中的每个element并执行您提供的回调 function。 That callback function takes a few arguments, the first argument is the element with would be 1 or 3 or 4 , the second argument is the index which starts at zero and goes up the length of the array -1.该回调 function 需要一些 arguments,第一个参数是元素,其134 ,第二个参数是从零开始并增加数组长度的索引 -1。 In the callback function used in your code example, the element is not used because it only cares about the index of each item but since the arguments still come in the same order you have to add the element as the first argument.在您的代码示例中使用的回调 function 中,未使用该元素,因为它只关心每个项目的索引,但由于 arguments 仍然以相同的顺序出现,您必须添加元素作为第一个参数。

here are some examples这里有些例子

 const arr = [1, 5, 6, 87, 2, 4, 7, 4, 6, 5] // get all the items that are divisible by 2 const filteredArr = arr.filter((element) => element % 2 == 0) console.log(filteredArr)

 const arr = [1, 5, 6, 87, 2, 4, 7, 4, 6, 5, 578, 23, 12, 1325, 78] // get all the items that are at an even index in the array and are greater than 10 const filteredArr = arr.filter((element, index) => index % 2 == 0 && element > 10) console.log(filteredArr)

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

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