Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find closest index of array in JavaScript
When working with arrays in JavaScript, you might need to find the index of the element that is numerically closest to a given target value. This is useful in scenarios like finding the nearest data point, closest price match, or similar proximity-based searches.
Problem Statement
Given an array of numbers and a target value, we need to find the index of the array element that has the smallest absolute difference from the target.
const arr = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362];
For example, if our target is 150, we need to find which array element is closest to 150 and return its index.
Solution
The approach is to iterate through the array, calculate the absolute difference between each element and the target, and keep track of the element with the minimum difference.
const arr = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362];
const closestIndex = (num, arr) => {
let curr = arr[0], diff = Math.abs(num - curr);
let index = 0;
for (let val = 0; val < arr.length; val++) {
let newdiff = Math.abs(num - arr[val]);
if (newdiff < diff) {
diff = newdiff;
curr = arr[val];
index = val;
}
}
return index;
};
console.log(closestIndex(150, arr));
console.log("Closest value:", arr[closestIndex(150, arr)]);
4 Closest value: 162
How It Works
The algorithm works by:
- Starting with the first element as the current closest value
- Calculating the absolute difference between the target and first element
- Iterating through each array element
- For each element, calculating its absolute difference from the target
- If a smaller difference is found, updating the closest index
- Returning the index of the element with minimum difference
Testing with Different Values
const arr = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362];
// Test different target values
console.log("Target 50, closest index:", closestIndex(50, arr));
console.log("Target 200, closest index:", closestIndex(200, arr));
console.log("Target 400, closest index:", closestIndex(400, arr));
// Show the actual closest values
console.log("For target 50, closest value:", arr[closestIndex(50, arr)]);
console.log("For target 200, closest value:", arr[closestIndex(200, arr)]);
console.log("For target 400, closest value:", arr[closestIndex(400, arr)]);
Target 50, closest index: 1 Target 200, closest index: 5 Target 400, closest index: 9 For target 50, closest value: 42 For target 200, closest value: 202 For target 400, closest value: 362
Optimized Version Using reduce()
Here's a more concise implementation using the reduce() method:
const arr = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362];
const closestIndexOptimized = (target, arr) => {
return arr.reduce((closestIdx, current, currentIdx) => {
return Math.abs(current - target) < Math.abs(arr[closestIdx] - target)
? currentIdx
: closestIdx;
}, 0);
};
console.log("Optimized version result:", closestIndexOptimized(150, arr));
Optimized version result: 4
Conclusion
Finding the closest index involves calculating absolute differences and tracking the minimum. Both the iterative approach and the reduce() method provide effective solutions for this common programming problem.
