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.

typescript

API

Parameters

Input parameters accepted by this util.

arr
required
ArrayLike<number>
A sorted (ascending) array-like of numbers, e.g. the cumulative heights of virtual list items.
val
required
number
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

typescript