This is the code I found as an answer to this question: Write a JavaScript program to remove duplicate items from an array (ignore case sensitivity).
这是我找到的回答这个问题的代码:编写一个JavaScript程序,从数组中删除重复的项(忽略大小写敏感)。
var arr1=[3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3, 'A'];
var uniqueArray = arr1.filter(function(elem,i,rep){
return i == rep.indexOf(elem);
})
console.log(uniqueArray);
I know what filter() does and that indexOf is used to find the index of the first occurence of an element, but I don't understand how this line:
我知道filter()做什么,index of用来查找元素第一次出现的索引,但是我不明白这一行是怎么回事:
i == rep.indexOf(elem);
introduces only the unique elements to uniqueArray.
唯一地介绍独特的元素到earray。
5 个解决方案
#1
2
According to MDN's article about Array#indexOf:
根据MDN关于数组#indexOf的文章:
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
indexOf()方法返回一个给定元素在数组中可以找到的第一个索引,如果不存在,则返回-1。
In the code you've presented:
在你提交的代码中:
var uniqueArray = arr1.filter(function(elem,i,rep){
return i == rep.indexOf(elem);
})
The variable i
in the filter's callback is the index of the current item in the array. If there is more than one appearance of an item, the current index (i
), and the index returned by Array#indexOf would be different for duplicated values after the 1st. In this case the filter callback will return false
, and the duplicated value will be filtered out.
过滤器回调中的变量i是数组中当前项的索引。如果一个条目的外观不止一个,那么当前索引(i)和数组#indexOf返回的索引对于第一个之后的重复值是不同的。在这种情况下,过滤器回调将返回false,重复的值将被过滤掉。
#2
2
i == rep.indexOf(elem);
will always return the first index under which the element has been found in the array, so only the first of any copies will be accepted in the filtered array.
将始终返回在数组中找到元素的第一个索引,因此在筛选后的数组中只接受第一个副本。
#3
0
The return is testing for i being equal to the index of the found element, so the function return will be true or false.
返回是为了测试i是否等于找到的元素的索引,因此函数返回将是真或假。
#4
0
var uniqueArray = arr1.filter(function(elem,i,rep){
return i == rep.indexOf(elem);
})
here elem would contain each element of the array, i would be the index of the current elem and rep would contain the whole array
now rep.indexOf(elem); would always give the index of the first occurrence of the element now Array.prototype.filter() works in a way that if you return true, it would not filter it but if you return false, it would filter it out so every element except the first occurrence gets filtered out
这里elem将包含数组的每个元素,i将是当前elem的索引,rep将包含现在的整个数组rep.indexOf(elem);如果你返回true,它就不会过滤它,但是如果你返回false,它会过滤掉所有的元素,除了第一次出现的元素被过滤掉?
#5
0
From w3schools
从w3schools
array.filter(function(currentValue, index, arr), thisValue)
数组中。过滤器(函数(arr currentValue、索引),thisValue)
In the .filter
method from your example, the function is iterating through each index and using indexOf
to find as you said: "the index of the first occurrence."
在示例中的.filter方法中,函数遍历每个索引并使用indexOf查找如您所说的:“第一次出现的索引”。
During the iteration, the index i
comes across repeated elements, and you use a test to see if this i
is equal to the index of the current element in the given array.
在迭代过程中,索引i遇到重复的元素,您可以使用一个测试,看看这个i是否等于给定数组中当前元素的索引。
From the w3schools:
从w3schools:
The filter() method creates an array filled with all array elements that pass a test (provided as a function).
filter()方法创建一个数组,其中包含通过测试的所有数组元素(作为函数提供)。
The test in your example is i == rep.indexOf(elem);
.
您的示例中的测试是i = rep.indexOf(elem);
When the iteration reaches index 2
, the element is a
. However, the element a
's first occurrence is at index 1
. At this point, i=2
but rep.indexOf(elem) = 1
. The test returns false
, because 2==1
is false
.
当迭代达到索引2时,元素是a,但是元素a的第一次出现在索引1处。此时,i=2,但rep.indexOf(elem) = 1。测试返回false,因为2==1是假的。
This false
return excludes the repeated element a
at index 2
from the newly created array.
这个错误返回将索引2处的重复元素a从新创建的数组中排除。
#1
2
According to MDN's article about Array#indexOf:
根据MDN关于数组#indexOf的文章:
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
indexOf()方法返回一个给定元素在数组中可以找到的第一个索引,如果不存在,则返回-1。
In the code you've presented:
在你提交的代码中:
var uniqueArray = arr1.filter(function(elem,i,rep){
return i == rep.indexOf(elem);
})
The variable i
in the filter's callback is the index of the current item in the array. If there is more than one appearance of an item, the current index (i
), and the index returned by Array#indexOf would be different for duplicated values after the 1st. In this case the filter callback will return false
, and the duplicated value will be filtered out.
过滤器回调中的变量i是数组中当前项的索引。如果一个条目的外观不止一个,那么当前索引(i)和数组#indexOf返回的索引对于第一个之后的重复值是不同的。在这种情况下,过滤器回调将返回false,重复的值将被过滤掉。
#2
2
i == rep.indexOf(elem);
will always return the first index under which the element has been found in the array, so only the first of any copies will be accepted in the filtered array.
将始终返回在数组中找到元素的第一个索引,因此在筛选后的数组中只接受第一个副本。
#3
0
The return is testing for i being equal to the index of the found element, so the function return will be true or false.
返回是为了测试i是否等于找到的元素的索引,因此函数返回将是真或假。
#4
0
var uniqueArray = arr1.filter(function(elem,i,rep){
return i == rep.indexOf(elem);
})
here elem would contain each element of the array, i would be the index of the current elem and rep would contain the whole array
now rep.indexOf(elem); would always give the index of the first occurrence of the element now Array.prototype.filter() works in a way that if you return true, it would not filter it but if you return false, it would filter it out so every element except the first occurrence gets filtered out
这里elem将包含数组的每个元素,i将是当前elem的索引,rep将包含现在的整个数组rep.indexOf(elem);如果你返回true,它就不会过滤它,但是如果你返回false,它会过滤掉所有的元素,除了第一次出现的元素被过滤掉?
#5
0
From w3schools
从w3schools
array.filter(function(currentValue, index, arr), thisValue)
数组中。过滤器(函数(arr currentValue、索引),thisValue)
In the .filter
method from your example, the function is iterating through each index and using indexOf
to find as you said: "the index of the first occurrence."
在示例中的.filter方法中,函数遍历每个索引并使用indexOf查找如您所说的:“第一次出现的索引”。
During the iteration, the index i
comes across repeated elements, and you use a test to see if this i
is equal to the index of the current element in the given array.
在迭代过程中,索引i遇到重复的元素,您可以使用一个测试,看看这个i是否等于给定数组中当前元素的索引。
From the w3schools:
从w3schools:
The filter() method creates an array filled with all array elements that pass a test (provided as a function).
filter()方法创建一个数组,其中包含通过测试的所有数组元素(作为函数提供)。
The test in your example is i == rep.indexOf(elem);
.
您的示例中的测试是i = rep.indexOf(elem);
When the iteration reaches index 2
, the element is a
. However, the element a
's first occurrence is at index 1
. At this point, i=2
but rep.indexOf(elem) = 1
. The test returns false
, because 2==1
is false
.
当迭代达到索引2时,元素是a,但是元素a的第一次出现在索引1处。此时,i=2,但rep.indexOf(elem) = 1。测试返回false,因为2==1是假的。
This false
return excludes the repeated element a
at index 2
from the newly created array.
这个错误返回将索引2处的重复元素a从新创建的数组中排除。