ArrayFindFirst
Return first item in array that matches the predicate function.
users = [ { name: "Ada" }, { name: "Grace" } ];
users.findFirst( ( user ) => user.name == "Grace" ); // { name: "Grace" }
users.findFirst( ( user ) => user.name == "Linus", "Unknown" ); // "Unknown"
Method Signature
ArrayFindFirst(array=[array], callback=[function], defaultValue=[any], parallel=[boolean], maxThreads=[any], virtual=[boolean])Arguments
array
array
true
The array to get the first item from.
callback
function
true
The function to invoke for each item. The function will be passed 3 arguments: the value, the index, the array. You can alternatively pass a Java Predicate which will only receive the 1st arg.
defaultValue
any
false
The default value to use if the array is empty or no value is returned from the predicate function.
parallel
boolean
false
Whether to run the filter in parallel. Defaults to false. If true, the filter will be run in parallel using a ForkJoinPool.
false
maxThreads
any
false
The maximum number of threads to use when running the filter in parallel. If not passed it will use the default number of threads for the ForkJoinPool. If parallel is false, this argument is ignored. If a boolean is provided it will be assigned to the virtual argument instead.
virtual
boolean
false
If true, the function will be invoked using virtual threads. Defaults to false. Ignored if parallel is false.
false
Examples
Find the first element matching a predicate
Returns the first element for which the callback returns true.
Result: Grace
With a default value when nothing matches
Result: Unknown
Find the first even number
Result: 6
Related
Last updated
Was this helpful?
