CFLib.org – Common Function Library Project

ReGroupStructureBy([mydata], col)

Last updated May 26, 2003

author

Casey Broich

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
Pass an Array of structures and the name of a column that exists within each, and it will create a Grouped "Structure of Array of Structures".

Return Values:
Returns a structure.

Example:

<cfscript>
mydata = arraynew(1);

mydata[1] = structnew();
mydata[1].age = 25;
mydata[1].gender = "m";
mydata[1].state = "ca";

mydata[2] = structnew();
mydata[2].age = 25;
mydata[2].gender = "m";
mydata[2].state = "ca";

mydata[3] = structnew();
mydata[3].age = 25;
mydata[3].gender = "f";
mydata[3].state = "ca";

mydata[4] = structnew();
mydata[4].age = 30;
mydata[4].gender = "m";
mydata[4].state = "wa";

mydata[5] = structnew();
mydata[5].age = 30;
mydata[5].gender = "m";
mydata[5].state = "wa";

mydata[6] = structnew();
mydata[6].age = 30;
mydata[6].gender = "f";
mydata[6].state = "wa";
</cfscript>

<cfset newdata = ReGroupBy(mydata,"state")>

<cfdump var="#newdata#">

Parameters:

Name Description Required
mydata Structure to parse. No
col Column to group by. Yes

Full UDF Source:

/**
 * Pass an Array of structures and the name of a column that exists within each, and it will create a Grouped &quot;Structure of Array of Structures&quot;.
 * 
 * @param mydata 	 Structure to parse. (Optional)
 * @param col 	 Column to group by. (Required)
 * @return Returns a structure. 
 * @author Casey Broich (cab@pagex.com) 
 * @version 1, May 26, 2003 
 */
function ReGroupBy(mydata,col){
  var i = "";
  var sttemp = structnew();
  var thisValue = "";
  for (i=1; i LTE arraylen(mydata); i=i+1){
    thisValue = mydata[i][col];
    if (not structkeyexists(sttemp, thisValue)){
      sttemp[thisValue] = arraynew(1);
    }
    arrayappend(sttemp[thisValue] , mydata[i]);
  }
  return sttemp;
}
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