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;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson