CFLib.org – Common Function Library Project

structToKeys(struct[, parent])

Last updated September 20, 2012

author

Adam Cameron

Version: 1 | Requires: CF9 | Library: UtilityLib

Description:
Traverses as struct and returns an array of all its keys with their full dotted path

Return Values:
Returns an array of keys with their fully-qualified dotted paths

Example:

// 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");

Parameters:

Name Description Required
struct A structure to extract the keys from Yes
parent A prefix to apply to each key No

Full UDF Source:

/**
 * Extracts the keys from a struct
 * v1.0 by Adam Cameron (thanks to Simon Baynes for code review)
 * 
 * @param struct 	 A structure to extract the keys from (Required)
 * @param parent 	 A prefix to apply to each key (Optional)
 * @return Returns an array of keys with their fully-qualified dotted paths 
 * @author Adam Cameron (adamcameroncoldfusion@gmail.com) 
 * @version 1.0, September 20, 2012 
 */
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])){
			result.addAll(structToKeys(struct[key], thisPath));
		}
	}
	return result;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson