Created
December 8, 2025 13:11
-
-
Save trycf/78504033df4ed2516fa0f450008f5a43 to your computer and use it in GitHub Desktop.
TryCF Gist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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