rc-js-util
Version:
A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.
135 lines (64 loc) • 2.08 kB
Markdown
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) > [rc-js-util](./rc-js-util.md) > [arrayBinaryIndexOf](./rc-js-util.arraybinaryindexof.md)
## arrayBinaryIndexOf() function
Performs a bisection search of an 'indexable' object, i.e. can be accessed by index, for example `Array`<!-- -->. Custom data structures are also supported.
**Signature:**
```typescript
export declare function arrayBinaryIndexOf<T>(indexable: T, comparisonValueToSearchFor: number, getComparisonValueAtIndex: TGetComparisonValueAtIndex<T>, length: number, start?: number): number;
```
## Parameters
<table><thead><tr><th>
Parameter
</th><th>
Type
</th><th>
Description
</th></tr></thead>
<tbody><tr><td>
indexable
</td><td>
T
</td><td>
The thing to be searched. This must be sorted ascending.
</td></tr>
<tr><td>
comparisonValueToSearchFor
</td><td>
number
</td><td>
The comparison value which is being searched for.
</td></tr>
<tr><td>
getComparisonValueAtIndex
</td><td>
[TGetComparisonValueAtIndex](./rc-js-util.tgetcomparisonvalueatindex.md)<!-- --><T>
</td><td>
A function that provides the value for comparison at a given index.
</td></tr>
<tr><td>
length
</td><td>
number
</td><td>
The number of elements in the structure `indexable` to search.
</td></tr>
<tr><td>
start
</td><td>
number
</td><td>
_(Optional)_ The start index.
</td></tr>
</tbody></table>
**Returns:**
number
The index of the searched for item, else -1 if it cannot be found.
## Remarks
The `indexable` parameter must be sorted ascending. Where there are multiple equal values the lowest index will be returned.
See [arrayBinaryIndexOf()](./rc-js-util.arraybinaryindexof.md)<!-- -->.
## Example
```typescript
// searching for the number 3 with start index 1 & length 2
const index = arrayBinaryIndexOf([1, 2, 3, 4], 3, (a, i) => a[i], 2, 1);
// index is 2
```