Created
December 8, 2025 13:11
-
-
Save trycf/8db545f14e6d62315e412fd43c85831f 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