Join Two Arrays in ColdFusion

19,933

Solution 1

Not really, but guess what, just use Java! :)

<cfset foo = [1,2,3]>
<cfset bar = [4,5,6]>
<cfset foo.addAll( bar )>

reference: Java's Collection Interface API.

source: http://www.aliaspooryorik.com/blog/index.cfm/e/posts.details/post/merging-two-arrays-267

Solution 2

CF10+, use

arrayAppend(array1, array2, true);

https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-a-b/arrayappend.html

Solution 3

If you're using Railo, you can use ArrayMerge (E.g. <cfset NewArray=ArrayMerge(FirstArray,SecondArray)>).

Solution 4

Its kinda dumb how coldfusion misses many basic functions that one would expect from a scripting language. Here's one I had to write quickly.

<cffunction name="mergeArrays" returntype="array" >
    <cfargument name="array1" type="array" required="true" >
    <cfargument name="array2" type="array" required="true" >

    <cfset arrayResult = arrayNew(1) >
    <cfloop array="#array1#" index="elem">
        <cfset arrayAppend(arrayResult,elem) >
    </cfloop>

    <cfloop array="#array2#" index="elem">
        <cfset arrayAppend(arrayResult,elem) >
    </cfloop>

    <cfreturn arrayResult>
</cffunction>

Solution 5

In CF 10 or Railo 4, you can use the concat() function of the Underscore.cfc library to get a new array that is a concatenation of two other arrays (without modifying the existing arrays). Example cfscript:

newArray = _.concat([1], [2]);

Result:

// newArray == [1, 2]

Using this method to get a new array is a bit cleaner than creating a new array and calling ArrayAppend on it twice.

(Disclaimer: I wrote Underscore.cfc)

Share:
19,933
Yisroel
Author by

Yisroel

Updated on June 05, 2022

Comments

  • Yisroel
    Yisroel almost 2 years

    Is there a built-in way to join two arrays in ColdFusion, similar to JavaScript's array.concat()?

  • Henry
    Henry almost 14 years
    I've added to Adobe's ColdFusion Bug Tracker as feature request at cfbugs.adobe.com/cfbugreport/flexbugui/cfbugtracker/… . Vote it up! :)
  • Henry
    Henry almost 14 years
    ArrayConcat Vs. ArrayMerge Vs ArrayAppend ? Please discuss here: groups.google.com/group/cfml-conventional-wisdom/browse_thre‌​ad/…
  • zarko.susnjar
    zarko.susnjar almost 14 years
    Oddly enough, underlying Java methods do not work always as expected. I still haven't figure out exactly when and why. I often use Java methods for removing duplicates, joining and sorting Arrays, I remember sometimes it didn't work depending how you create arrays, which operations you perform before calling Java method etc. So pay attention!
  • Leigh
    Leigh over 11 years
    @zarko.susnjar - I know this is an old thread, but the reason for the unexpected results with methods like removeAll, retainAll, etcetera is that the java methods are not as lenient as your typical CF function. They are usually data type sensitive. So "2" (string) and val(2) (number) are considered different values/elements. Unless you are absolutely certain the data types in both arrays match, you are better of using other methods IMO.
  • zarko.susnjar
    zarko.susnjar over 11 years
    Of course, if you are not certain about something, always go the safer and proven way.
  • John
    John over 11 years
    +1, It is impressive that you remembered this question and came back two years later to improve it.
  • Leigh
    Leigh over 11 years
    Actually I think you got it from me, haha .. and looking at this thread, I probably got it from @noj's answer and rewrote it in cfscript ;)
  • Leigh
    Leigh about 11 years
    Just keep in mind it is not a complete equivalent as it will a) drop any empty array elements and b) split array values on whatever delimiter is used ,. ie The single value arr[1] ="Doe, John" would become arr[1] = Doe, arr[2] = John.
  • Betty Mock
    Betty Mock about 11 years
    Leigh, you make a good point. This could be worked around by using a different list delimiter, such as ~~~, or ~!~!~! which would not likely appear within the array.
  • Leigh
    Leigh about 11 years
    True, though multiple characters will not not work in this case. In most versions of CF, multiple characters are treated as separate delimiters. ie ~! means CF sees the values as delimited by ~ OR !. (Things may have changed in CF10). Using a single character like ascii 30, and preserving empty list elements, should get it much closer.
  • Henry
    Henry about 10 years
    CF10+ user, pls see my other answer.