find_index

This function returns the index of the first item in a list or tuple that passes the test. Otherwise nil is returned.

This function does not generate a change.

Function

array.find_index(callback)

Arguments

Argument Type Description
callback closure The statement to try.

Explanation of the callback argument:

Iterable Callback arguments Description
array item, index Iterate over items in the array. Both item and index are optional.

Return value

The index of the first item in the array that passes the test; otherwise, nil is returned.

Example

This code shows an example using find_index():

// some sports as an example
sports = ['cycling', 'baseball', 'running', 'tennis', 'skateboarding'];

// return the index of `running` in list
sports.find_index(|sport| sport == 'running');

Return value in JSON format

2