arr.append()vs. arr.push()在coldfusion中

发布于 01-20 17:37 字数 157 浏览 4 评论 0原文

有什么区别

<cfscript>
   i = []
    
   i.push(1)

   i = []

   i.append(1)
</cfscript>

他们俩似乎都有相同的结果。

What is the difference between

<cfscript>
   i = []
    
   i.push(1)

   i = []

   i.append(1)
</cfscript>

?

They both seem to have the same results.

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

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

发布评论

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

评论(2

梦过后2025-01-27 17:37:21

除了詹姆斯·A·莫勒(James A.对于append(),还有一个其他可选的布尔参数MERGE,如果设置为true(默认)将合并到源数组。如果为false,它将在末尾添加数组作为附加元素。对于您将单个元素附加到数组中的示例,请将Merge参数设置为True或False什么都没有更改。但是,如果您将2个阵列附加在一起,则差异很明显。例如

<cfscript>
   i=[1,2,3,4,5];
   i.append([6,7], true);
   writeDump(i);

   i=[1,2,3,4,5];
   i.append([6,7], false);
   writeDump(i);

   i=[1,2,3,4,5];
   i.push([6,7]); // Works the same as append(..., false);
   writeDump(i);
</cfscript>

编辑(来自James A. Mohler的评论)

结果
i.append([6,7],true); [1,2,3,4,5,6,7]
i.append([6,7],false); [1,2,3,4,5,[6,7]]
i.push([6,7]); [1,2,3,4,5,[6,7]

您可以看到GIST

In addition to James A. Mohler's answer where the return value is different for each function, there's another distinction between the two. For append(), there's also an additional optional boolean parameter merge which if set to true (default) will merge to the source array. If false, it will add the array as an additional element at the end. For your example of appending a single element to the array, setting the merge parameter to either true or false changes nothing. However, if you're appending 2 arrays together, the difference is clear. For example

<cfscript>
   i=[1,2,3,4,5];
   i.append([6,7], true);
   writeDump(i);

   i=[1,2,3,4,5];
   i.append([6,7], false);
   writeDump(i);

   i=[1,2,3,4,5];
   i.push([6,7]); // Works the same as append(..., false);
   writeDump(i);
</cfscript>

EDIT (from James A. Mohler's comment)

Results
i.append([6,7], true); [1,2,3,4,5,6,7]
i.append([6,7], false); [1,2,3,4,5,[6,7]]
i.push([6,7]); [1,2,3,4,5,[6,7]]

You can see the gist here.

时光磨忆2025-01-27 17:37:21

如果你运行这个

<cfscript>
    
    i = []
    
    writedump(i.push(1)) // returns array length
    writedump(i.append(1)) // returns array
    
</cfscript>

你可以看到他们给出了不同的响应。

输入图片此处描述

If you run this

<cfscript>
    
    i = []
    
    writedump(i.push(1)) // returns array length
    writedump(i.append(1)) // returns array
    
</cfscript>

You can see that they give different responses.

enter image description here

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