StructFindKey
Searches a struct for a given key and returns an array of values
Method Signature
StructFindKey(struct=[structloose], key=[any], scope=[string])
Arguments
struct
struct
true
The struct to search
key
any
true
The key to search for
scope
string
false
Either one (default), which finds the first instance or all to return all values
one
Examples
Find first match for a nested struct key
Beatles = {
PERSON1 : {
ID : 1,
FIRSTNAME : "John",
LASTNAME : "Lennon"
},
PERSON2 : {
ID : 2,
FIRSTNAME : "Paul",
LASTNAME : "McCartney"
},
PERSON3 : {
ID : 3,
FIRSTNAME : "George",
LASTNAME : "Harrison"
},
PERSON5 : {
ID : 5,
FIRSTNAME : "Abbey",
LASTNAME : "Road"
},
PERSON4 : {
ID : 4,
FIRSTNAME : "Ringo",
LASTNAME : "Starr"
}
};
myKey = structFindKey( Beatles, "lastName", "one" );
writeOutput( JSONSerialize( myKey ) );
Result: [{"path":".PERSON3.LASTNAME","owner":{"LASTNAME":"Harrison","FIRSTNAME":"George","ID":3},"value":"Harrison"}]
Find all matches of a nested struct key
Beatles = {
PERSON1 : {
ID : 1,
FIRSTNAME : "John",
LASTNAME : "Lennon"
},
PERSON2 : {
ID : 2,
FIRSTNAME : "Paul",
LASTNAME : "McCartney"
},
PERSON3 : {
ID : 3,
FIRSTNAME : "George",
LASTNAME : "Harrison"
},
PERSON4 : {
ID : 4,
FIRSTNAME : "Ringo",
LASTNAME : "Starr"
}
};
myKey = structFindKey( Beatles, "lastName", "all" );
writeOutput( JSONSerialize( myKey ) );
Result: [{"path":".PERSON3.LASTNAME","owner":{"LASTNAME":"Harrison","FIRSTNAME":"George","ID":3},"value":"Harrison"},{"path":".PERSON1.LASTNAME","owner":{"LASTNAME":"Lennon","FIRSTNAME":"John","ID":1},"value":"Lennon"},{"path":".PERSON2.LASTNAME","owner":{"LASTNAME":"McCartney","FIRSTNAME":"Paul","ID":2},"value":"McCartney"},{"path":".PERSON4.LASTNAME","owner":{"LASTNAME":"Starr","FIRSTNAME":"Ringo","ID":4},"value":"Starr"}]
Find first match for a nested struct key using member function
CF11+ calling the findKey member function on a struct.
Beatles = {
PERSON1 : {
ID : 1,
FIRSTNAME : "John",
LASTNAME : "Lennon"
},
PERSON2 : {
ID : 2,
FIRSTNAME : "Paul",
LASTNAME : "McCartney"
},
PERSON3 : {
ID : 3,
FIRSTNAME : "George",
LASTNAME : "Harrison"
},
PERSON5 : {
ID : 5,
FIRSTNAME : "Abbey",
LASTNAME : "Road"
},
PERSON4 : {
ID : 4,
FIRSTNAME : "Ringo",
LASTNAME : "Starr"
}
};
myKey = Beatles.findKey( "lastName", "one" );
writeOutput( JSONSerialize( myKey ) );
Result: [{"path":".PERSON3.LASTNAME","owner":{"LASTNAME":"Harrison","FIRSTNAME":"George","ID":3},"value":"Harrison"}]
Additional Examples
animals = {
COW : {
NOISE : "moo",
SIZE : "large"
},
PIG : {
NOISE : "oink"
},
CAT : {
NOISE : "meow",
SIZE : "small"
}
};
// Show all animals
Dump( label="All animals", var=animals );
// Find "all" animal(s) containing key of 'size'
findAnimalsWithSize = StructFindKey( animals, "size", "all" );
// Show results in findAnimalsWithSize
Dump( label="Results of StructFindKey(animals, ""size"", ""all"")", var=findAnimalsWithSize );
Related
Last updated
Was this helpful?