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.
array.find_index(callback)
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. |
The index of the first item in the array that passes the test;
otherwise, nil
is returned.
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