arrayOfStructsToStructsOfStructs(array, key)
Last updated June 11, 2009
Version: 0 | Requires: ColdFusion MX | Library: DataManipulationLib
Description:
This UDF accepts an array of structures and converts it to a structure of structures.
It accepts an array of structures and a key as mandatory arguments. The key must exist in all structures in the array
Return Values:
Returns a struct.
Example:
<cfset stStruct1 = structNew()>
<cfset stStruct1.name = "Micheal">
<cfset stStruct1.age = 20>
<cfset stStruct2 = structNew()>
<cfset stStruct2.name = "Smidt">
<cfset stStruct2.age = 25>
<cfset aArray[1] = stStruct1>
<cfset aArray[2] = stStruct2>
<cfset stStructOfStructs = ArrayOfStructsToStructsOfStructs(array=aArray,key="name"))>
<cfdump var="#stStructofStructs#">
Parameters:
| Name | Description | Required |
|---|---|---|
| array | An array of structs. | Yes |
| key | A key value to use for the new structure. Must exist in all structs in the array. | Yes |
Full UDF Source:
<!---
Converts an array of structures to an structure of structures,
@param array An array of structs. (Required)
@param key A key value to use for the new structure. Must exist in all structs in the array. (Required)
@return Returns a struct.
@author Tayo Akinmade (olusina@hotmail.com)
@version 0, June 11, 2009
--->
<cffunction name="ArrayOfStructsToStructsOfStructs" access="public" output="false" returntype="struct" hint="Converts an array of structs to an struct of structs">
<cfargument name="array" type="array" required="true" hint="An array of structures">
<cfargument name="key" type="string" required="true" hint="Key to use">
<cfscript>
var stStructOfStructs = structNew();
var i = 0;
// loop over array
for(i=1;i lte arrayLen(arguments.array);i=i+1){
stStructOfStructs[arguments.array[i][arguments.key]] = arguments.array[i];
}
return stStructOfStructs;
</cfscript>
</cffunction>
Search CFLib.org
Latest Additions
Tayo Akinmade added
arrayTrim
12 day(s) ago
Will Belden added
longTime
17 day(s) ago
James Sleeman added
quickSort
27 day(s) ago
Ben Forta added
GetHostAddress
30 day(s) ago
Top Rated
EksporSQLData
Rated 5.0, 16 time(s)
backupDatabase
Rated 5.0, 13 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)