Join Two Arrays in ColdFusion

强颜欢笑 提交于 2019-11-29 04:47:13

问题


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


回答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




回答2:


CF10+, use

arrayAppend(array1, array2, true);

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




回答3:


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




回答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>



回答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)




回答6:


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>



回答7:


You can easily concatenate two lists like this:

<cfset combolist = ListAppend(lista,listb, ",")>

So, first convert your two arrays to lists using ArrayToList(). Combine the two lists with the ListAppend() and then convert the answer back to an array with ListToArray().

I don't know how efficient this is, but the code is very simple. I'd love to use the arrayAppend() but I'm in ColdFusion 8.




回答8:


I took this from Ben Nadel and used it to perform encryption and hashing. Worked like a charm!

<cfscript>
    // Note: BinaryDecode/CharsetDecode return java arrays. 
    // Unlike CF arrays, java arrays are immutable, 
    // so the Java addAll(..) method to merge arrays won't work here. 
    // http://stackoverflow.com/a/10760835/104223

    // function to merge immutable arrays the long way
    function mergeArrays( array1, array2 ){
        var i = 0;
        var newArray = [];
        for (i = 1; i <= arrayLen(arguments.array1); i++) {
            arrayAppend(newArray, arguments.array1[i]);
        }
        for (i = 1; i <= arrayLen(arguments.array2); i++) {
            arrayAppend(newArray, arguments.array2[i]);
        }
        return newArray;
    }


    //convert the saltArray string and CustomerID string to UTF-8 byte arrays.
    saltByteArray = charsetDecode(salt, "utf-8");
    CustomerIdByteArray = charsetDecode(CustomerId, "utf-8");

    //create a new byte array consisting of the CustomerId bytes
    //appended with the salt bytes by merging the two binary arrays 
    //via custom function, mergeArrays
    mergedBytes = mergeArrays( CustomerIdByteArray, saltByteArray );
</cfscript>


来源:https://stackoverflow.com/questions/3081756/join-two-arrays-in-coldfusion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!