views:

197

answers:

3

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

+7  A: 

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

Henry
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!
zarko.susnjar
+2  A: 

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

Gert G
I've added to Adobe's ColdFusion Bug Tracker as feature request at http://cfbugs.adobe.com/cfbugreport/flexbugui/cfbugtracker/main.html#bugId=83397 . Vote it up! :)
Henry
ArrayConcat Vs. ArrayMerge Vs ArrayAppend ? Please discuss here: http://groups.google.com/group/cfml-conventional-wisdom/browse_thread/thread/95a4b511128c37ae
Henry
A: 

In javascript array.join(s) creates a string out of all of the elements of the array separated by the delimiter s. A similar function to this in ColdFusion is the ArrayToList function. As far as appending an array to another I don't believe there is a CF function for that. Check http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions-pt0_03.html#3473387 to see the list of Array functions in CF. Or try something like this:

<cfscript>
   for(index = 1; index LTE ArrayLen(array2); i = i + 1) {
      ArrayAppend(array1, array2[i]);
  }
</cfscript>
Joel Anderson
thanks, i meant array.concat. I fixed the question
Yisroel