es-stl - v3.0.2
    Preparing search index...
    • Finds the greatest index whose projected numeric value is less than or equal to searchValue.

      Type Parameters

      • T

        Element type stored in the array.

      Parameters

      • array: T[]

        Sorted array to search.

      • searchValue: number

        Numeric value to locate.

      • callback: SearchCallback<T>

        Projects each element to its comparable numeric value.

      Returns number

      The greatest matching lower-bound index, or -1 before the first element.

      This is useful for locating the lower bound segment of a half-open range sequence such as [array[i], array[i + 1]). The array must already be sorted in ascending order by the callback value. When searchValue is lower than the first projected value, the function returns -1.

      const dataList = [
      { value: 0 },
      { value: 100 },
      { value: 300 },
      { value: 700 },
      ];

      binarySearchInRange(dataList, 125, (data) => data.value);
      // => 1
      binarySearchInRange([10, 20, 30], 5, (value) => value);
      // => -1
      binarySearchInRange([10, 20, 30], 20, (value) => value);
      // => 1