findIndex
arrayReturns the index of the first element predicate returns truthy for instead of the element itself.
Signature
findIndex<T>(array: T[], predicate: (value: T) => boolean, fromIndex?: number): numberParameters
| Name | Type | Description |
|---|---|---|
array | T[] | The array to inspect |
predicate | (value: T) => boolean | The function invoked per iteration |
fromIndex? | number | The index to search fromDefault: 0 |
Returns
number - Returns the index of the found element, else -1
Examples
Find user
import { findIndex } from 'dashlite'
const users = [
{ user: 'barney', active: false },
{ user: 'fred', active: false },
{ user: 'pebbles', active: true }
]
const result = findIndex(users, (o) => o.user === 'barney')
console.log(result)Output:
0Available since version 1.0.0