ArrayFind
Array finders and contains functions with and without case sensitivity.
Please note that "contain" methods return a boolean, while "find" methods return an index. If you use a function as the value, it will be used as a search closure or lambda. The signature of the function should be:
( value, index ) => {
return true; // if the value is found, else false
}
Example:
array = [ 1, 2, 3, 4, 5 ];
index = array.find( ( value, index ) -> {
return value == 3;
} );
We recommend you use BoxLang lambdas ({@code ->}) for this purpose, so they only act upon the value and index without any side effects. They will be faster and more efficient.
Method Signature
ArrayFind(array=[array], value=[any], substringMatch=[boolean])
Arguments
array
array
true
The array to be searched.
value
any
true
The value to find or a closure to be used as a search function.
substringMatch
boolean
false
If true, the search will be a substring match. Default is false. This only works on simple values, not complex ones. For that just use a function filter.
false
Examples
Find an "Apple" in an array of fruit
Returns the index of the element "Apple" in the array
writeOutput( arrayFindNoCase( [
"orange",
"pineapple",
"apple"
], "Apple" ) );
Result: 3
arrayFind is not Case Sensitive
Not case sensitive so "Apple" will be found in the array, returns 1. Use arrayFind for case sensitive matching.
writeOutput( arrayFindNoCase( [
"orange",
"pineapple",
"apple"
], "Apple" ) );
Result: 1
Member Functions: Find an "Apple" in an array of fruit
Calls the findNoCase member function of the array object.
fruit = [
"orange",
"pineapple",
"apple"
];
writeOutput( fruit.findNoCase( "Apple" ) );
Result: 3
Additional Examples
arrVariable = [
"Water",
"Sky",
2,
"Air"
];
WriteDump( ArrayFindNoCase( arrVariable, 5 ) ); // Outputs 0
WriteDump( ArrayFindNoCase( arrVariable, "air" ) ); // Outputs 4
WriteDump( ArrayFindNoCase( arrVariable, 2 ) );
// Outputs 3
Related
Last updated
Was this helpful?