目录
Get the Index of the Max value in an Array in JavaScript
在 JavaScript 中获取数组中最大值的索引
获取数组中最大值的索引:
- 使用
Math.max()
方法获取数组中的最大值。 - 使用该
String.indexOf()
方法获取最大值的索引。 - 该
indexOf()
方法返回数组中值的索引,或者-1
如果找不到该值。
const arr = [3, 5, 8, 100, 20]; const max = Math.max(...arr); const index = arr.indexOf(max); console.log(index); // 👉️ 3
如果您需要获取最小值的索引,请改用该Math.min()
函数。
const arr = [10, 5, 0, 15, 30]; const min = Math.min(...arr); const index = arr.indexOf(min); console.log(index); // 👉️ 2
我们使用Math.max()方法来获取数组中的最大值。
该Math.max()
方法需要多个以逗号分隔的数字作为参数,因此我们不能直接将数组传递给它。
const max = Math.max(3, 5, 8) console.log(max) // 👉️ 8
我们使用
扩展运算符 (…)来解压缩数组的值,并将它们作为多个以逗号分隔的参数传递给该Math.max()
方法。
const arr = [3, 5, 8, 100, 20]; const max = Math.max(...arr); const index = arr.indexOf(max); console.log(index); // 👉️ 3
最后一步是使用
Array.indexOf
方法获取值的索引max
。
indexOf
您可以使用for
循环来获取数组中所有最大值的索引。
获取数组中所有具有最大值的元素的索引
获取数组中所有具有最大值的元素的索引:
- 使用
Math.max()
方法获取数组中的最大值。 - 声明一个
indexes
存储空数组的变量。 - 遍历数组并仅将最大值的索引推送到数组
indexes
。
const arr = [3, 5, 8, 100, 20, 100]; const max = Math.max(...arr); const indexes = []; for (let index = 0; index < arr.length; index++) { if (arr[index] === max) { indexes.push(index); } } console.log(indexes) // 👉️ [3, 5]
我们使用 for 循环来迭代 forarray.length
迭代。
indexes
。该数组存储数组中indexes
所有出现的值的索引。max
您也可以使用该Array.reduce()
方法。
# Get the Index of the Max value in an Array using Array.reduce()
This is a three-step process:
- Use the
Array.reduce()
method to iterate over the array. - Check if the current element is greater than the accumulator.
- If the condition is met, return the current index, otherwise, return the
accumulator.
const arr = [3, 5, 8, 100, 20]; const index = arr.reduce((accumulator, current, index) => { return current > arr[accumulator] ? index : accumulator; }, 0); console.log(index); // 👉️ 3
The function we passed to the Array.reduce()
method gets called with the
accumulator value, the current array element and the current index.
Array.reduce()
is used as the initial value for the accumulator
variable.We used 0
as the initial value.
Whatever we return from the function gets set as the new value of the
accumulator variable.
On each iteration, we check if the current list item is greater than the element
at the accumulator
index.
If the condition is met, the current index is returned, otherwise, the
accumulator
index is returned.
After the last iteration, the index
variable stores the index of the max value
in the array.
# Get the Index of the Min value in an Array in JavaScript
To get the index of the min value in an array:
- Use the
Math.min()
method to get the min value in the array. - Use the
Array.indexOf()
method to get the index of the min value. - The
indexOf
method returns the index of the first occurrence of the value
in the array.
const arr = [10, 5, 0, 15, 30]; const min = Math.min(...arr); const index = arr.indexOf(min); console.log(index); // 👉️ 2
We used the
spread (…) operator when
we called the
Math.min()
method.
Math.min()
method expects comma-separated numbers as arguments, so we can’t directly pass it an array.const min = Math.min(10, 5, 0) console.log(min) // 👉️ 0
We used the spread operator (…) to unpack the values of the array and passed
them as multiple, comma-separated arguments to the Math.min()
method.
We then used the
Array.indexOf
method to find the index of the first occurrence of the min
value.
const arr = [10, 5, 0, 15, 30]; const min = Math.min(...arr); const index = arr.indexOf(min); console.log(index); // 👉️ 2
0
, the indexOf
method would return the index of the first occurrence.If you need to
find the index of all occurrences
of the min
value in the array, use a for
loop.
const arr = [10, 5, 0, 15, 30, 0, 0]; const min = Math.min(...arr); console.log(min); // 👉️ 0 const indexes = []; for (let i = 0; i < arr.length; i++) { if (arr[i] === min) { indexes.push(i); } } console.log(indexes); // 👉️ [2, 5, 6]
We used the Mah.min()
method to find the min
value in the array and used a
for
loop to iterate over the array.
On each iteration, we check if the current array element is equal to the min
value.
If the condition is met, we push the current index into the indexes
array.
The indexes
array stores the index of all occurrences of the min
value in
the array.
You can also use the Function.apply
method as an alternative to the spread
(…) operator.
# Get the Index of the Min value in an Array using Function.apply()
This is a three-step process:
- Use the
Function.apply()
method in the call to theMath.min()
method. - Use the
Array.indexOf()
method to get the index of the min value. - The
indexOf
method returns the index of the first occurrence of the value
in the array.
const arr = [10, 5, 0, 15, 30]; // 👇️ now using apply, instead of ... const min = Math.min.apply(null, arr); const index = arr.indexOf(min); console.log(index); // 👉️ 2
The only difference in the code is how we get the min value in the array.
The arguments we passed to the
Function.apply()
method are:
- 争论
this
– 就我们的目的而言,它是无关紧要的。 - 其值将
Math.min()
作为多个逗号分隔参数传递给方法的数组。
在幕后,该apply
方法解压缩数组的值并将它们作为多个参数传递给调用该方法的函数。
额外资源
您可以通过查看以下教程来了解有关相关主题的更多信息: