在 ColdFusion 中连接两个数组

发布于 2024-09-06 01:36:07 字数 68 浏览 3 评论 0 原文

ColdFusion 中有没有内置的方法来连接两个数组,类似于 JavaScript 的 array.concat() ?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

薄暮涼年 2024-09-13 01:36:08

我从 Ben Nadel 那里得到了这个并用它来执行加密和散列。效果非常好!

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

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>
缪败 2024-09-13 01:36:08

是的,ColdFusion(10+) 有一个内置函数可以附加两个数组。

脚本版本:array1.append(array2, true);

标签版本:arrayAppend(array1, array2, true);代码>

Yes, ColdFusion(10+) has an inbuilt function to append two arrays.

Script version : array1.append(array2, true);

Tag version: arrayAppend(array1, array2, true);

笑脸一如从前 2024-09-13 01:36:07

不是真的,但你猜怎么着,只要使用 Java 就可以了! :)

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

参考: Java 的集合接口 API

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

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

冰雪之触 2024-09-13 01:36:07

如果您使用 Railo,则可以使用 ArrayMerge (例如 )。

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

ぃ弥猫深巷。 2024-09-13 01:36:07

Coldfusion 错过了人们期望从脚本语言获得的许多基本功能,这有点愚蠢。这是我必须快速写的一篇。

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

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>
沧桑㈠ 2024-09-13 01:36:07

在 CF 10 或 Railo 4 中,您可以使用 Underscore.cfc 库 获取一个由其他两个数组串联而成的新数组(不修改现有数组)。 cfscript 示例:

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

结果:

// newArray == [1, 2]

使用此方法获取新数组比创建新数组并对其调用 ArrayAppend 两次要干净一些。

(免责声明:我写了Underscore.cfc)

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)

猫腻 2024-09-13 01:36:07

在 javascript 中, array.join(s) 创建一个由分隔符 s 分隔的数组所有元素组成的字符串。 ColdFusion 中与此类似的函数是 ArrayToList 函数。至于将一个数组附加到另​​一个数组,我不相信有 CF 函数可以做到这一点。检查 http://livedocs.adobe .com/coldfusion/8/htmldocs/help.html?content=functions-pt0_03.html#3473387 查看 CF 中的数组函数列表。或者尝试这样的事情:

<cfscript>
   for(index = 1; index LTE ArrayLen(array2); i = i + 1) {
      ArrayAppend(array1, array2[i]);
  }
</cfscript>

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>
不疑不惑不回忆 2024-09-13 01:36:07

您可以轻松连接两个列表,如下所示:

因此,首先使用 ArrayToList()< 将两个数组转换为列表< /代码>。使用 ListAppend() 将两个列表合并,然后使用 ListToArray() 将答案转换回数组。

我不知道这有多高效,但是代码很简单。我很想使用 arrayAppend() 但我使用的是 ColdFusion 8。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文