While programming in JavaScript, there can be a requirement to sort an array in an ascending or descending manner, especially while solving mathematical problems. For instance, in the case of locating multiple entries and sorting them. In such scenarios, getting the index of the min value in an array in JavaScript is very helpful in accessing, locating, and sorting the data.
This tutorial will discuss the approaches to get the min value in an array in JavaScript.
How to Get the Index of the Min Value in an Array in JavaScript?
The min value’s index in an array in JavaScript can be fetched by applying the following approaches:
The mentioned approaches will now be illustrated one by one!
Approach 1: Get the Index of the Min Value in an Array in JavaScript Using Math.min() and indexOf() Methods
The “Math.min()” method returns the number having the most minimum value. Whereas the “indexOf()” method outputs the index of the specified array element and returns “-1” if not found. These methods can be utilized to compute the minimum value from an array first and then return its corresponding index.
Syntax
In the given syntax:
“num1, num2” represent the numbers that need to be compared.
In the above syntax:
“search” corresponds to the index of the fetched array element.
Example 1: Get the Min Value’s Index in an Array in JavaScript Using the Math.min() Method, indexOf() Method and spread Operator
Let’s overview the below-stated example:
let array = [20, 40, 10];
let minimum = Math.min(...array);
let minIndex = array.indexOf(minimum);
console.log("The index of the minimum value is:", minIndex);
</script>
In the above code snippet:
- Declare an array having contained values in it.
- After that, apply the “Math.min()” method. In its parameter, apply the “spread” operator to unpack the array values and fetch the minimum value.
- Finally, apply the “indexOf()” method to return the corresponding minimum value’s index in an array.
Output
In the above output, it can be observed that the minimum value’s index from an array is retrieved.
Example 2: Get the Min Value’s Index in an Array in JavaScript Using Math.min(), indexOf() and apply() Methods
Let’s follow the below-given example:
let array = [10, 20, 30];
let minimum = Math.min.apply(null, array);
let minIndex = array.indexOf(minimum);
console.log("The index of the minimum value is:", minIndex);
</script>
Perform the following steps in the above code snippet:
- Likewise, declare an array having the stated values.
- In the next step, apply the “Math.min()” and “apply()” methods in combination. This will result in taking the arguments in the form of an array and returning the minimum of the values.
- Finally, apply the “indexOf()” method to return the corresponding index against the minimum value extracted in the previous step.
Output
From the above output, it can be seen that the index against the minimum value “10” is retrieved.
Approach 2: Get the Index of the Min Value in an Array in JavaScript Using for Loop
This approach can be implemented to iterate along the array values and compare each of the values to get the minimum value and return its index.
Example
Let’s overview the following example:
const array = [30, 20, 50, 70, 10, 40, 17];
let value = array[0];
let minIndex = 0;
for (let i= 0; i < array.length; i++) {
if (array[i] < value) {
minIndex = i;
}}
console.log("The index of the minimum value is:", minIndex);
</script>
In the above code snippet:
- Declare an array of the stated values.
- In the next step, refer to the first array value and initialize the value of the index as well.
- Further, apply the “for” loop to iterate along the array items.
- The loop will check for the minimum value with respect to the first array value.
- It will continue to iterate till the most minimum value is accessed referring to the “if” condition.
- Resultantly, the value of the index will be updated according to the fetched minimum value from an array.
Output
The desired requirement is achieved in the above output.
Approach 3: Get the Index of the Min Value in an Array in JavaScript Using reduce() Method
The “reduce()” method executes a reducer function for array elements. This method can be applied to check for each array value for the minimum value such that the array will be reduced to the minimum value’s index.
Syntax
In the above syntax:
- “function” refers to the function to be executed for each array element.
- “total” corresponds to the initial value.
- “value” is the current element.
- “index” points to the current element’s index.
- “array” is the element’s array.
- “this” indicates the value to be passed to the function.
Example
Let’s follow the below-given example:
let array = [2, 4, 1];
let minIndex = array.reduce((index, value, i, array) =>
value < array[index] ? i : index, 0
);
console.log("The index of the minimum value is:", minIndex);
</script>
Let’s move toward the explanation of the above code:
- Declare an array likewise.
- In the next step, apply the “reduce()” method having the stated parameters.
- Here, a condition will be applied to the element’s indexes in an array, and the array will be reduced down to the minimum value’s index.
- Finally, after the successful check, the index of the minimum value will be displayed.
Output
In the above output, it can be observed that the minimum value’s index is displayed.
Conclusion
The Math.min() and indexOf() methods can be applied to compute the minimum value from an array and then return its corresponding index via the spread operator or the apply() method. The for-loop approach can access the array values by iterating along them and comparing each of the values to get the minimum value’s index. The reduce method can be applied by reducing the array till the minimum value is retrieved. This blog explained how to get the min value’s index in an array in JavaScript.