diff --git a/src/common/utils/number.ts b/src/common/utils/number.ts index 9a4a4e7a2..117aa4066 100644 --- a/src/common/utils/number.ts +++ b/src/common/utils/number.ts @@ -46,7 +46,16 @@ export function binarySearchNearest(dataList: T[], valueKey: keyof T, targetV break } } - return left + let nearestIndex = left + let nearestDistance = Math.abs(Number(dataList[left][valueKey]) - Number(targetValue)) + for (let i = left + 1; i <= right; i++) { + const distance = Math.abs(Number(dataList[i][valueKey]) - Number(targetValue)) + if (distance < nearestDistance) { + nearestDistance = distance + nearestIndex = i + } + } + return nearestIndex } /**