CFLib.org – Common Function Library Project

arrayOfStructsToStructsOfStructs(array, key)

Last updated June 11, 2009

author

Tayo Akinmade

Version: 0 | Requires: CF6 | 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 aArray = arrayNew(1)>

<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

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