admin管理员组文章数量:1401849
I am new to JS and was trying to learn how to properly work with indexOf in JS, that is, if you look at the code below:
var sandwiches = ['turkey', 'ham', 'turkey', 'tuna', 'pb&j', 'ham', 'turkey', 'tuna'];
var deduped = sandwiches.filter(function (sandwich, index) {
return sandwiches.indexOf(sandwich) === index;
});
// Logs ["turkey", "ham", "tuna", "pb&j"]
console.log(deduped);
I am trying to remove duplicates but wanted to ask two questions. Firstly, in here return sandwiches.indexOf(sandwich) === index; why we need to use "== index;". Secondly, since indexOf returns index like 0, 1 or 2 ... then why when we console.log(deduped) we get array of names instead of array of indexes. Hope you got my points
I am new to JS and was trying to learn how to properly work with indexOf in JS, that is, if you look at the code below:
var sandwiches = ['turkey', 'ham', 'turkey', 'tuna', 'pb&j', 'ham', 'turkey', 'tuna'];
var deduped = sandwiches.filter(function (sandwich, index) {
return sandwiches.indexOf(sandwich) === index;
});
// Logs ["turkey", "ham", "tuna", "pb&j"]
console.log(deduped);
I am trying to remove duplicates but wanted to ask two questions. Firstly, in here return sandwiches.indexOf(sandwich) === index; why we need to use "== index;". Secondly, since indexOf returns index like 0, 1 or 2 ... then why when we console.log(deduped) we get array of names instead of array of indexes. Hope you got my points
Share Improve this question edited Sep 14, 2019 at 11:19 Dickens asked Sep 14, 2019 at 11:14 DickensDickens 9154 gold badges12 silver badges27 bronze badges 05 Answers
Reset to default 6You use a method of Javascript Array that is filter
, this method take a function that returns a boolean.
The function filter
returns a new Array based on the function passed applied to each entry.
If the function return true, then the entry is included in the new Array, otherwise is discarded.
As the functions check the indexOf an entry to be the current index is true for the first occurrency of the entry.
All the duplications will fail the expression as they are not the first index found by indexOf, so they are discarded.
since the logic is to remove the duplicates from the array, in your example, you have "turkey" as duplicates. the "turkey" exists in position 0,2,6 so whenever you call indexOf("turkey") always returns 0 because the indexOf function returns the first occurrence of a substring. so for the elements in position 2 & 6 the condition fails. then it won't return that element.
That is how the filter works in javascript. it evaluates the condition and returns true or false that indicates whether an element to be included in the new array or not, in your example the condition is
return sandwiches.indexOf(sandwich) === index;
Perhaps the basic logic is easier to see at a glance if you use arrow notation:
const deduped = myArray => myArray.filter((x, i) => myArray.indexOf(x) === i);
The key point is that indexOf
returns the index of the first occurrence of x
. For that occurrence the result of the parison will be true hence the element will be retained by the filter. For any subsequent occurrence the parison will be false and the filter will reject it.
Difference between
===
(identity) and==
(equality): if type of pared values are different then===
will return false, while==
will try to convert values to the same type. So, in cases where you pare some values with known types it is better to use===
. (http://www.c-point./javascript_tutorial/jsgrpComparison.htm)You get as result an array of names instead of array of indexes because
Array.filter
do not change the values, but only filter them. The filter function in your case isreturn sandwiches.indexOf(sandwich) === index;
which returntrue
orfalse
. If you want get the indexes of your items after deduplication, then usemap
after filter:
a.filter(...).map(function(item, idx) {return idx;})
@Dikens, indexOf
gives the index of the element if found. And if the element is not found then it returns -1
.
In your case you are filtering the array and storing the values in the deduped
. That's why it is showing an array.
If you console the indexOf in the filter function then it will log the index of the element.
For example :
var deduped = sandwiches.filter(function (sandwich, index) {
console.log(sandwiches.indexOf(sandwich));
return sandwiches.indexOf(sandwich) === index;
});
本文标签: javascriptConfusion with how indexOf works in JSStack Overflow
版权声明:本文标题:javascript - Confusion with how indexOf works in JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744275252a2598379.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论