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

Signature TypeScript complète, extraite du fichier util.

typescript

API

Paramètres

Paramètres d'entrée acceptés par cet util.

arr
requis
ArrayLike<number>
A sorted (ascending) array-like of numbers, e.g. the cumulative heights of virtual list items.
val
requis
number
The target value to search for. The function returns the last index whose element does not exceed this value.

API

Valeur de retour

La valeur retournée par cet util.

Index of the closest element ≤ val, or the last index when val exceeds all elements.

Usage

Exemples

Extraits de code prêts à coller.

Find the closest cumulative offset

typescript