Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function similar to ArrayFind from ColdFusion 9 in ColdFusion 8?

I was talking with another fellow programmer at work and we use ColdFusion. He is telling me simply look for a value in an array I have to do a whole loop? Is it true there is no function in ColdFusion 8 to simply look for a value in an Array?

like image 494
Darren Avatar asked Dec 07 '22 00:12

Darren


1 Answers

arrayFind() doesn't exist in ColdFusion 8. However, you don't need to loop. There are two ways:

Take advantage of the fact that ColdFusion arrays implement the interface java.util.List:

<cfset valueToFind = 1>
<cfset array = [1,2,3]>
<!--- add one because CF does 1 based vs. Java 0 based arrays --->
<cfset position = array.indexOf(valueToFind) + 1> 

Use list operations:

<cfset valueToFind = 1>
<cfset array = [1,2,3]>
<cfset position = listFind(arrayToList(array), valueToFind)>

The first (Java List) method is faster.

like image 84
orangepips Avatar answered Jun 02 '23 21:06

orangepips