findIndex
description
Finds an item's index in a Collection
using a predicate function. The predicate function has the following signature: predicate :: (Any value, Any index, Collection collection) => Boolean
. It should return a Boolean
is called with the following arguments:
value: the item's value
index: the item's index or key. For arrays, the index will be a
Number
, for objects and maps it will be aString
, and for sets, it will be equal to the item's valuecollection: the collection being iterated on.
If no item matching the predicate is found in the collection, findIndex
will return null
.
The predicate function can be asynchronous, which will turn the result into a Promise
, requiring you to use await
(or Promise.prototype.then
).
examples
basic example
searching in other data structures
We're using findIndex
on data structures which are not arrays, and we are still looking for the index of the word 'world'
. Here, words
is a Set
, so the associated index is equal to the value, which is world
. translator
is an Object
, and the key associated to the 'world'
is the string 'mundo'
. otherTranslator
is a Set
, translating French to English, and the key associated to 'world'
is 'monde'
.
using an asynchronous predicate
Here, we have an asynchronous predicate which takes a character_id and checks if the character is R2-D2, using the Star Wars API. Since the predicate is asynchronous, we need to use await
because the result is a Promise
. Luckily, we found the droid we were looking for 😎.
Last updated