在 JavaScript 中获取数组中最大/最小值的索引

目录

Get the Index of the Max value in an Array in JavaScript

  1. 获取数组中最大值的索引
  2. 获取数组中最小值的索引

在 JavaScript 中获取数组中最大值的索引

获取数组中最大值的索引:

  1. 使用Math.max()方法获取数组中的最大值。
  2. 使用该String.indexOf()方法获取最大值的索引。
  3. indexOf()方法返回数组中值的索引,或者-1如果找不到该值。
索引.js
const arr = [3, 5, 8, 100, 20]; const max = Math.max(...arr); const index = arr.indexOf(max); console.log(index); // 👉️ 3

如果您需要获取最小值的索引,请改用该Math.min()函数。

索引.js
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()方法需要多个以逗号分隔的数字作为参数,因此我们不能直接将数组传递给它。

索引.js
const max = Math.max(3, 5, 8) console.log(max) // 👉️ 8

我们使用
扩展运算符 (…)来解压缩数组的值,并将它们作为多个以逗号分隔的参数传递给该Math.max()方法。

索引.js
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循环来获取数组中所有最大值的索引。

获取数组中所有具有最大值的元素的索引

获取数组中所有具有最大值的元素的索引:

  1. 使用Math.max()方法获取数组中的最大值。
  2. 声明一个indexes存储空数组的变量。
  3. 遍历数组并仅将最大值的索引推送到数组
    indexes
索引.js
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:

  1. Use the Array.reduce() method to iterate over the array.
  2. Check if the current element is greater than the accumulator.
  3. If the condition is met, return the current index, otherwise, return the
    accumulator.
index.js
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.

The second argument we passed to 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:

  1. Use the Math.min() method to get the min value in the array.
  2. Use the Array.indexOf() method to get the index of the min value.
  3. The indexOf method returns the index of the first occurrence of the value
    in the array.
index.js
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.

The Math.min() method expects comma-separated numbers as arguments, so we can’t directly pass it an array.
index.js
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.

index.js
const arr = [10, 5, 0, 15, 30]; const min = Math.min(...arr); const index = arr.indexOf(min); console.log(index); // 👉️ 2
If there were multiple array elements with the value of 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.

index.js
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:

  1. Use the Function.apply() method in the call to the Math.min() method.
  2. Use the Array.indexOf() method to get the index of the min value.
  3. The indexOf method returns the index of the first occurrence of the value
    in the array.
index.js
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:

  1. 争论this– 就我们的目的而言,它是无关紧要的。
  2. 其值将Math.min()作为多个逗号分隔参数传递给方法的数组。

在幕后,该apply方法解压缩数组的值并将它们作为多个参数传递给调用该方法的函数。

额外资源

您可以通过查看以下教程来了解有关相关主题的更多信息: