Created
September 18, 2012 15:57
-
-
Save adamcameron/3743900 to your computer and use it in GitHub Desktop.
Function to recursively get all structkeys and convert to dot notation
This file contains 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
<cfscript> | |
// data to test with | |
st = { | |
top1 = "top1 value", | |
top2 = { | |
middle = { | |
bottom1 = [1,2,3], | |
bottom2 = {}, | |
bottom3 = "bottom3 value" | |
} | |
}, | |
top3 = "top3 value" | |
}; | |
writeDump(var=st, label="source struct"); | |
// get the keys | |
keys = structToKeys(st, "st"); | |
arraySort(keys, "textnoCase"); // sort 'em to make 'em look nice | |
writeDump(var=keys, label="extracted keys"); | |
array function structToKeys(required struct struct, string parent=""){ | |
var result = []; | |
for (var key in struct){ | |
var thisPath = listAppend(parent, key, "."); | |
arrayAppend(result, thisPath); | |
if (isStruct(struct[key])){ | |
arrayAppend(result, structToKeys(struct[key], thisPath), true); | |
} | |
} | |
return result; | |
} | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above example returns an array, thus:
1 st.TOP1
2 st.TOP2
3 st.TOP2.MIDDLE
4 st.TOP2.MIDDLE.BOTTOM1
5 st.TOP2.MIDDLE.BOTTOM2
6 st.TOP2.MIDDLE.BOTTOM3
7 st.TOP3