sortedIndexOf

array

This method is like indexOf except that it performs a binary search on a sorted array.

Signature

sortedIndexOf<T>(array: T[], value: T): number

Parameters

NameTypeDescription
arrayT[]The sorted array to inspect
valueTThe value to search for

Returns

number - Returns the index of the matched value, else -1

Examples

Binary search

import { sortedIndexOf } from 'dashlite'

const result = sortedIndexOf([4, 5, 5, 5, 6], 5)
console.log(result)

Output:

1

Available since version 1.0.0