Last updated: Mar 1, 2024
Reading timeยท5 min
To get the index of the max value in an array:
Math.max()
method to get the max value in the array.String.indexOf()
method to get the index of the max value.indexOf()
method returns the index of the value in the array or -1
if
the value isn't found.const arr = [3, 5, 8, 100, 20]; const max = Math.max(...arr); const index = arr.indexOf(max); console.log(index); // ๐๏ธ 3
If you need to get the index of the min value, use the Math.min()
function
instead.
const arr = [10, 5, 0, 15, 30]; const min = Math.min(...arr); const index = arr.indexOf(min); console.log(index); // ๐๏ธ 2
We used the Math.max() method to get the max value in the array.
The Math.max()
method expects multiple, comma-separated numbers as arguments,
so we can't just pass it an array directly.
const max = Math.max(3, 5, 8) console.log(max) // ๐๏ธ 8
We used the
spread operator (...) to
unpack the values of the array and then we passed them as multiple,
comma-separated arguments to the Math.max()
method.
const arr = [3, 5, 8, 100, 20]; const max = Math.max(...arr); const index = arr.indexOf(max); console.log(index); // ๐๏ธ 3
The last step is to use the
Array.indexOf()
method to get the index of the max
value.
indexOf
method would return only the index of the first occurrence.You can use a for
loop to get the index of all max values in the array.
To get the indexes of all elements with the max value in an array:
Math.max()
method to get the max value in the array.indexes
variable that stores an empty array.indexes
array.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]
We used a for loop to iterate for array.length
iterations.
indexes
array.The indexes
array stores the index of all occurrences of the max
value in
the array.
You can also use the Array.reduce()
method.
This is a three-step process:
Array.reduce()
method to iterate over the array.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.
To get the index of the min value in an array:
Math.min()
method to get the min value in the array.Array.indexOf()
method to get the index of the min value.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.
This is a three-step process:
Function.apply()
method in the call to the Math.min()
method.Array.indexOf()
method to get the index of the min value.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 minimum value in the array.
The arguments we passed to the Function.apply() method are:
this
argument - for our purposes it is irrelevant.Math.min()
method as multiple,
comma-separated arguments.Under the hood, the apply
method unpacks the values of the array and passes
them as multiple arguments to the function the method is called on.
You can learn more about the related topics by checking out the following tutorials: