[英]indexOf on Javascript not working as expected on Array
I want to iterate through an Array to find the index of the highest numbers on it, then write those index numbers on another new Array. 我想遍历一个数组以找到其上最高编号的索引,然后将这些索引号写在另一个新的Array上。
I came up with this code: 我想出了以下代码:
let scores = [60, 50, 60, 58, 54, 54, 58, 50, 52, 54, 48, 69, 34, 55, 51, 52, 44, 51, 69, 64, 66, 55, 52, 61, 46, 31, 57, 52, 44, 18, 41, 53, 55, 61, 51, 44];
let highestScore = Math.max(...scores);
let topScores = [];
for (score of scores) {
if (score == highestScore) {
topScores.push(scores.indexOf(score));
}
}
console.log(topScores);
Then the result that the console shows is: 然后控制台显示的结果是:
topScores = [11, 11]
...when I expected: ...当我期望:
topScores = [11, 18]
as those are the positions where the highest numbers (both 69) are on the scores array. 因为这些是得分数组上最高的数字(均为69)的位置。
Can someone explain to me what's happening? 有人可以向我解释发生了什么吗? I searched but I can't come up with the issue. 我进行了搜索,但无法提出问题。 Thank you very much. 非常感谢你。
As mentioned by Fritz Array.indexOf(x)
always returns the first position of x in the array. 如Fritz所述, Array.indexOf(x)
始终返回x在数组中的第一个位置。 The first 69
is at index 11
. 前69
在索引11
。
You can use Array.forEach()
instead of for...of
: 您可以使用Array.forEach()
代替for...of
:
let scores = [60, 50, 60, 58, 54, 54, 58, 50, 52, 54, 48, 69, 34, 55, 51, 52, 44, 51, 69, 64, 66, 55, 52, 61, 46, 31, 57, 52, 44, 18, 41, 53, 55, 61, 51, 44]; let highestScore = Math.max(...scores); let topScores = []; scores.forEach((score, index) => { if (score == highestScore) { topScores.push(index); } }) console.log(topScores);
This is beacause indexOf
always return the first index at which element exist in array. 这是因为indexOf
总是返回数组中存在元素的第一个索引。 You can use reduce()
您可以使用reduce()
let scores = [60, 50, 60, 58, 54, 54, 58, 50, 52, 54, 48, 69, 34, 55, 51, 52, 44, 51, 69, 64, 66, 55, 52, 61, 46, 31, 57, 52, 44, 18, 41, 53, 55, 61, 51, 44]; let highestScore = Math.max(...scores); let res = scores.reduce((ac,a,i) => (a === highestScore && ac.push(i),ac),[]) console.log(res)
As others have already mentioned, both indexOf
and findIndex
always return the index of the first match. 正如其他人已经提到的那样, indexOf
和findIndex
总是返回第一个匹配项的索引。 But there is no need for using either because you can access the current index in for..of
like this: 但是不需要使用任何一种,因为您可以像这样访问for..of
的当前索引:
for (const [index, score] of scores.entries())
which allows you to simply do 这使您只需
topScores.push(index);
let scores = [60, 50, 60, 58, 54, 54, 58, 50, 52, 54, 48, 69, 34, 55, 51, 52, 44, 51, 69, 64, 66, 55, 52, 61, 46, 31, 57, 52, 44, 18, 41, 53, 55, 61, 51, 44]; let highestScore = Math.max(...scores); let topScores = []; for (const [index, score] of scores.entries()) { if (score == highestScore) { topScores.push(index); } } console.log(topScores);
the .indexOf()
returns only the index of the first occurrence of the particular ignore the other indexes
so in this case, indexOf
won't work. .indexOf()
只返回第一次出现的特定索引,而忽略其他indexes
因此在这种情况下, indexOf
将不起作用。 Just loop
through and push
the index of the value equal to the maximum number to a new array
只需loop
遍历并将等于最大值的值的索引push
送到新array
let scores = [60, 50, 60, 58, 54, 54, 58, 50, 52, 54, 48, 69, 34, 55, 51, 52, 44, 51, 69, 64, 66, 55, 52, 61, 46, 31, 57, 52, 44, 18, 41, 53, 55, 61, 51, 44]; let noReplica = Math.max(...scores) let ret = []; for (let i = 0, length = scores.length; i < length; i++) { if (scores[i] === noReplica) { ret.push(i) } } console.log(ret)
scores.indexOf(score) always returns the first position of score in the scores. scores.indexOf(score)始终返回分数在分数中的第一位。
Use following code if you need the index. 如果需要索引,请使用以下代码。
for (let i = 0; i < scores.length; i ++) {
let score = scores[i];
if (score == highestScore) {
topScores.push(i);
}
}
console.log(topScores);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.