findLastIndex

array

This method is like findIndex except that it iterates over elements of collection from right to left.

Signature

findLastIndex<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 from

Returns

number - Returns the index of the found element, else -1

Examples

Find from right

import { findLastIndex } from 'dashlite'

const users = [
  { user: 'barney', active: true },
  { user: 'fred', active: false },
  { user: 'pebbles', active: false }
]
const result = findLastIndex(users, (o) => o.user === 'pebbles')
console.log(result)

Output:

2

Available since version 1.0.0