Merging Two Structs in ColdFusion

The following script showing how to merge 2 ColdFusion structure into one. The following sample showing 2 difference structure which each has two keys

tech.david-cheong.com

<!--- Struct1 have 2 key: FirstName and Last time --->
<cfset struct1={firstname="Adam" ,lastname="Presley" }>

<!--- Struct 2 have 2 key: FirstName and age --->
<cfset struct2={ firstname="Michael" ,age="33" }>

<!--- Using a nifty ColdFusion method we can mash the two together in a single line of code. --->

<cfset structAppend(struct1, struct2) /> <!--- <cfset struct1.putAll(struct2) /> --->

<cfdump var="#struct1#">

tech.david-cheong.com
Notice the commented out version. That is the underlying method to do the same thing as StructAppend.

tech.david-cheong.com

The structure result after merge

Leave a Reply

%d bloggers like this: