Coldfusion Javascript Friendly Query to Array function.
Ben Nadel posted a simple query to Array function that made an update to and thought I would share.
Original: Converting a Query to an Array
component {
/*****************************************************************************
* Description:
* Convert a coldfusion query to a JSON array of structures.
* Adapted from: http://www.bennadel.com/blog/124-Ask-Ben-Converting-a-Query-to-an-Array.htm
* Input:
* Query
*
* Output:
* Array
*/
public array function queryToArray( any q ) {
var QtA = StructNew();
if( isQuery( q ) ) {
QtA.Columns = ListToArray( ARGUMENTS.qry.ColumnList );
QtA.qry = q;
} else {
QtA.Columns = ListToArray( q.getPrefix().COLUMNLIST );
QtA.qry = q.getResult();
}
// Create an array that will hold the query equivalent.
QtA.QueryArray = ArrayNew( 1 );
// Loop over the query.
for (QtA.RowIndex = 1 ; QtA.RowIndex LTE QtA.qry.RecordCount ; QtA.RowIndex = (QtA.RowIndex + 1)){
QtA.Row = StructNew();
// Loop over the columns in this row.
for (QtA.ColumnIndex = 1 ; QtA.ColumnIndex LTE ArrayLen( QtA.Columns ) ; QtA.ColumnIndex = (QtA.ColumnIndex + 1)){
// Get a reference to the query column.
QtA.ColumnName = lcase( QtA.Columns[ QtA.ColumnIndex ] );
// Store the query cell value into the struct by key.
StructInsert( QtA.Row, QtA.ColumnName, QtA.Qry[ QtA.ColumnName ][ QtA.RowIndex ] );
}
// Add the structure to the query array.
ArrayAppend( QtA.QueryArray, QtA.Row );
}
// Return the array equivalent.
return( QtA.QueryArray );
}
}
Basic I changed the way that the structure is built to be able to control the variable's case. Thus making this version javascript friendly.
- Login to post comments
