Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save trycf/14ed64872786a96860b527eee714e72e to your computer and use it in GitHub Desktop.
Save trycf/14ed64872786a96860b527eee714e72e 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}];
function printTree(data, indent="", result=""){
for (child in data) { // First level
if(len(indent)) result &= indent & "└"; //only add the indent if a child
result &= child.id & "<br>"; //print id with a break
if(structKeyExists(child,"children")) //if there are children recurse with a deeper indent
result &= printTree(child["children"], indent & "&nbsp;&nbsp;");
}
return result;
}
echo(printTree(apidata));
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment