binaryClosest
Commons
stable
Binary search a sorted array of numbers to find the index of the element closest to (but not exceeding) a target value. Used internally by virtual scroll lists.
API
Signature
Full TypeScript signature sourced from the util file.
API
Parameters
Input parameters accepted by this util.
- arrrequiredArrayLike<number>
- A sorted (ascending) array-like of numbers, e.g. the cumulative heights of virtual list items.
- valrequirednumber
- The target value to search for. The function returns the last index whose element does not exceed this value.
API
Return value
The value returned by this util.
- Index of the closest element ≤ val, or the last index when val exceeds all elements.
Usage
Examples
Ready-to-paste usage snippets.
Find the closest cumulative offset