{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner

Merging two arrays

I've mentioned this before in passing in previous posts, but I've just used it to solve a problem and thought it deserved it's own blog post!

The problem I had was that I had two arrays that I want to merge into one. ColdFusion doesn't have an in-built function for merging arrays so you end up having to loop and append one element at a time, which is frankly rubbish! Here's an example:

<cfset foo = [1,2,3]>
<cfset bar = [4,5,6]>
<cfloop array="#bar#" index="i">
  <cfset ArrayAppend( foo, i )>

As we all know, ColdFusion is built on-top of Java, so we can take advantage of this and use some of the Java methods. In short we can get rid of the loop and just do this instead:

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

Much nicer!

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks