arrayFind

These functions searches the array for the specified object. Returns the index in the array of the first match, or 0 if there is no match.

arrayFind(array, object) → returns Numeric

Member Function Syntax

someArray.find(object)

Argument Reference for the arrayFind function

array

Required: Yes
The array you are searching.

object

Required: Yes
The value you are looking for in the array. CF 10+ or Lucee 4.5+ support passing a closure function in this argument as well.

Examples sample code invoking the arrayFind function


Find an "apple" in an array of Fruit

Returns the index of the element "apple" in the array

writeOutput( arrayFind( ["orange","pineapple","apple"], "apple" ));

Expected Result: 3


Member Functions: Find an "apple" in an array of Fruit

Calls the find member function of the array object. Requires CF 11+ or Lucee 4.5+

fruit = [ "orange", "pineapple", "apple" ];
writeOutput( fruit.find( "apple" ) );

Expected Result: 3


arrayFind is Case Sensitive

Case sensitive so "Apple" is not in the array, returns 0. Use arrayFindNoCase for case insensitive matching.

writeOutput( arrayFind(["orange","pineapple","apple"], "Apple") );

Expected Result: 0


Using arrayFind with a closure / callback function

The arrayFind function passes each array value into the supplied function. The function returns a boolean, true if you found what you are looking for. The result of the arrayFind function is the index of the array element that was found, or 0 if not found.

a = [5,4,3,2,1];
writeOutput(arrayFind(a, function(item) { return item == 2;}));

Fork me on GitHub