QueryReduce

Iterates over every row of the given query and calls the closure with that row. This function will reduce the query to a single value and will return this value.

Returns: any

Usage

QueryReduce( query, closure [, initalValue ] )

Arguments

Argument Description
query
(query, required)

query to iterate

closure
(function, required)

function/closure that implements the following constructor [function(any result, struct row[, number rowNumber, query query]):any].

initalValue
(object, optional)

inital value passed as part of the first closure call

Examples

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   |
*/

/* Documented way
totalAge = people.reduce( function(age, row, rowNumber, recordset ){
    return age +  DateDiff( 'yyyy', recordset.dob, Now() );
},0);
*/

// intitialise age in closure
totalAge = people.reduce( function(age=0, row, rowNumber, recordset ){
    return age +  DateDiff( 'yyyy', recordset.dob, Now() );
});


Dump( var=totalAge, label='people - total age' );

/* Output:
totalAge = 147
*/

See also