Quirrel4.41.0

types.Array.findindex

Method on every array
pure array.findindex(callback: function): any

Finds the index of the first element for which callback returns a true value.

The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing ... from the prose below, not from the signature.

Parameters

callbackfunctioncallback(value, [index], [array]), tested against each element, in order

Return value

The index of the first match, or null if none matched.

Errors

Whatever callback throws, on the first element where it throws.

Notes

callback gets exactly as many of the arguments listed above as it declares parameters for, and never more.

Use indexof instead when the target is a plain value to compare, rather than a condition to test.

Example

examples/types/array/findindex.nut
let a = [10, 20, 30];
println("a.findindex(v > 15) =", a.findindex(function(v) { return v > 15 }));
println("a.findindex(v > 999) =", a.findindex(function(v) { return v > 999 }));    // not found -> null
Output:
a.findindex(v > 15) = 1
a.findindex(v > 999) = null

See also

findvalueFinds the first element for which predicate returns a true value.
indexofFinds the index of the first element that equals value.
arrayclass index