queryReduce
Reduces query columns just like in array or collection
queryReduce(query, function(result, row [, currentRow] [, query]){} [, initialVal]) → returns any
Argument Reference
query query Required
query to process entries from
callback any Required
Closure or a function reference that will be called for each of the iteration.
Callback parameters:
-
result* any : The result of the reduce operation after the previous iteration -
row* struct : A struct with all of the columns for the current iteration -
currentRow* numeric : The value for the current iteration -
query* query : A reference of the original query
initialValue any
initial value passed as part of the first closure call
Links more information about queryReduce
- Map, Reduce and other Higher Order Functions - A Primer for map, reduce, filter, and Higher Order Functions in general.
- ColdFusion 2016: query iteration functions - A look at the higher-order functions that queries have
Examples Sample code invoking the queryReduce function
Sum one query column
fruits = queryNew("fruit,amount",
"varchar,integer", [{
"fruit": "apples",
"amount": 15
}, {
"fruit": "pineapples",
"amount": 3
}, {
"fruit": "strawberries",
"amount": 32
}]
);
total_fruits = queryReduce(fruits,function(a,b) {
return a + b.amount;
},0);
writeOutput(total_fruits); Expected Result: 50
© 2012–present Foundeo, Inc. and the CFDocs contributors.
Licensed under the MIT License.
ColdFusion is a trademark of Adobe Systems Incorporated.
https://cfdocs.org/queryreduce