3 functions to extend Coldfusion array
Array is a data type that exists in almost any programming languages. Although Coldfusion has provided many useful functions to work with arrays including some quite unique to Coldfusion such as ArrayToList, ArrySort, etc., I find many are still missing. For example, there is no function to remove duplicated items (huh?) and I use this one a lot. What about a function to reverse an array? Guess what, it's not there either (at least not natively in coldfusion). So I went ahead and made my own ones.
Below are 3 functions I created (some by myself, some combined with what I found on the Internet) to extend the capabilities of arrays in Coldfusion. Hope you will find them useful and feel free to use them in your projects.
1. ArrayUnique
What it does: remove duplicated items from an array.
How to use it: <cfset newArray = ArrayUnique(orginalArray)>
<cffunction name="ArrayUnique" access="public" returntype="array">
<cfargument name="arr" type="array" required="yes" hint="The original array" />
<cfset var result = ArrayNew(1)>
<!---
Create a linked hashset java object as it has:
1) unique key and
2) order
--->
<cfset lhs = createObject("java", "java.util.LinkedHashSet").init(arguments.arr)>
<cfset result = lhs.toArray()>
<cfreturn result>
</cffunction>
Example:
2. ArrayReverse
What it does: reverse the order of an array.
How to use it: <cfset newArray = ArrayReverse(orginalArray)>
<cffunction name="ArrayReverse" access="public" returntype="array"> <cfargument name="arr" type="array" required="yes" hint="The first array" /> <cfset var result = ArrayNew(1)> <cfloop array="#arguments.arr#" index="i"> <cfset arrayPrepend(result, i)> </cfloop> <cfreturn result> </cffunction>
Example:
3. ArrayUnion
What it does: union 2 arrays. Create a new array containing items that appear in either or both of the arrays.
How to use it: <cfset newArray = ArrayUnion(array1, array2)>
This function the same as the Union operator in mathematics. Read more about union operator if you are not familiar with it.
<cffunction name="ArrayUnion" access="public" returntype="array"> <cfargument name="arr1" type="array" required="yes" hint="The first array" /> <cfargument name="arr2" type="array" required="yes" hint="The second array" /> <cfset var result = ArrayNew(1)> <!--- As Coldfusion is built on top of Java, we can make use of the addAll() method in java to append one array to another ---> <cfset result.addAll(arguments.arr1)> <cfset result.addAll(arguments.arr2)> <cfset result = arrayUnique(result)> <cfreturn result> </cffunction>
Example:
4. Download
These functions are part of the core.cfc library in cfTrigger. But I put them here into a separate cfc for you to easily download. I hope you find these functions useful and thanks for reading. If you have any questions, feel free to ask in the comments below.
Download the CFC


very helpful. thanks for posting this!