Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save trycf/78504033df4ed2516fa0f450008f5a43 to your computer and use it in GitHub Desktop.

Select an option

Save trycf/78504033df4ed2516fa0f450008f5a43 to your computer and use it in GitHub Desktop.
TryCF Gist
<cfset employee = [
{id=1, name="sara", dept="it"},
{id=1, name="sara", dept="it"},
{id=1, name="sara", dept="dc"},
{id=1, name="sara", dept="finance"}
]>
<cfset deptStruct = {}>
<!--- Group employees by department --->
<cfloop array="#employee#" index="emp">
<cfif NOT structKeyExists(deptStruct, emp.dept)>
<cfset deptStruct[emp.dept] = []>
</cfif>
<cfset arrayAppend(deptStruct[emp.dept], emp)>
</cfloop>
<cfdump var="#deptStruct#">
<!--- Convert to JSON --->
<cfoutput>
#serializeJSON(deptStruct)#
</cfoutput>
✅ Output (Exactly What You
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment