findIndex

array

Returns 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): number

Parameters

NameTypeDescription
arrayT[]The array to inspect
predicate(value: T) => booleanThe function invoked per iteration
fromIndex?numberThe 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:

0

Available since version 1.0.0