Skip to content

Instantly share code, notes, and snippets.

@webonix
Created October 12, 2015 04:08
Show Gist options
  • Save webonix/74fe0be194d27d1d80c8 to your computer and use it in GitHub Desktop.
Save webonix/74fe0be194d27d1d80c8 to your computer and use it in GitHub Desktop.
query member function - reduce()
<cfscript>
people = QueryNew( "name,dob,age", "varchar,date,int", [
[ "Susi", CreateDate( 1970, 1, 1 ), 0 ],
[ "Urs" , CreateDate( 1995, 1, 1 ), 0 ],
[ "Fred", CreateDate( 1960, 1, 1 ), 0 ],
[ "Jim" , CreateDate( 1988, 1, 1 ), 0 ]
]);
Dump( var=people, label="people - origional query" );
/* Output:
| name | dob | age |
------------------------------------
| Susi | 1970-01-01 00:00:00 | 0 |
| Urs | 1995-01-01 00:00:00 | 0 |
| Fred | 1960-01-01 00:00:00 | 0 |
| Jim | 1988-01-01 00:00:00 | 0 |
*/
totalAge = people.reduce(function(result, row, rowNumber, qryData){
return result + DateDiff('yyyy', row.dob, Now());
}, 0);
dump(var=totalAge, label='total age of all people');
/* Output:
147
*/
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment