arrayFind

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

arrayFind(array, value) → returns numeric

Member Function Syntax

someArray.find(value)

Argument Reference

array array
Required

The array you are searching.

value any
Required

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.

Compatibility

ColdFusion:

Version 9+ CF10+ Added support for closure instead of value

Lucee:

Lucee4.5+ Added support for closure instead of value

Examples
Sample code invoking the arrayFind function

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

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

Expected Result: 3

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

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

CF 10+ Lucee 4.5+ 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 first matching array element found, or 0 if not found. Use arrayFindAll to return an array of all found item indexes.

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

Expected Result: 4

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub