Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save trycf/65e57bf1afb3991a73fa2171cbbf8239 to your computer and use it in GitHub Desktop.
Save trycf/65e57bf1afb3991a73fa2171cbbf8239 to your computer and use it in GitHub Desktop.
TryCF Gist
<cfscript>
// Single parents
//apiData = [{"id":13},{"id":16},{"id":17},{"id":18},{"id":14},{"id":15}];
// parents with one nested level of children
//apiData = [{"id":16},{"id":13,"children":[{"id":14}]},{"id":17,"children":[{"id":15}]},{"id":18}];
// parents with two nested levels of children
apidata = [{"id":10},{"id":13,"children":[{"id":14,"children":[{"id":15}]}]},{"id":2,"children":[{"id":5,"value":"Item 5 value","foo":"Bar","children":[{"id":6},{"id":7},{"id":8}]},{"id":3},{"id":4}]},{"id":9},{"id":1},{"id":16},{"id":17}];
for (myStruct in apiData) { // First level
writeoutput(mystruct.id); writeoutput("<br>");
// Second level
if(structKeyExists(myStruct,"children")) {
if(isarray(mystruct.children)) {
for (child in mystruct.children) {
writeoutput(" - "); writeoutput(child.id); writeoutput(" (Parent of #mystruct.id#) <br>");
// 3rd level
if(structKeyExists(child,"children")) {
if(isarray(child.children)) {
for (child2 in child.children) {
writeoutput(" ---"); writeoutput(child2.id); writeoutput(" (Parent of #child.id#) <br>");
}
}
} // END 3rdlevel
}
}
} // END Second level
} // END First level
</cfscript>
<br><br>
<p>Tag version</p>
<!--- First level --->
<cfloop array="#apiData#" index="myStruct">
<cfif structKeyExists(myStruct,"id")>
<cfoutput>#mystruct.id#<br></cfoutput>
<!--- Second level --->
<cfif structKeyExists(myStruct,"children")>
<cfif isarray(mystruct.children)>
<cfoutput>
<cfloop array="#mystruct.children#" index="child">
- #child.id# (Parent of #mystruct.id#)<br>
<!--- 3rd level --->
<cfif structKeyExists(child,"children")>
<cfif isarray(child.children)>
<cfoutput>
<cfloop array="#child.children#" index="child2">
---- #child2.id# (Parent of #child.id#)<br>
</cfloop>
</cfoutput>
</cfif>
</cfif>
<!--- END 3rd level --->
</cfloop>
</cfoutput>
</cfif>
</cfif>
<!--- END Second level --->
</cfif>
</cfloop>
<!--- END First level --->
<br><br>
<cfdump var="#apidata#">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment