collectionReduce

Sum amount of an item value for every item

collectionReduce(collection, closure [, initialValue]) → returns any

This function requires Lucee.  Not supported on Adobe ColdFusion, etc.

Argument Reference

collection any
Required

closure function
Required

initialValue any

Examples
Sample code invoking the collectionReduce function

coll = [{
    id: 0,
    type: 'apples',
    amount: 25
},{
    id: 1,
    type: 'pinapples',
    amount: 2
},{
    id: 2,
    type: 'peaches',
    amount: 13
}];
fruits = collectionReduce(coll,function(a,b) {
    return a + b.amount;
},0);
writeOutput(fruits&' fruits');

Expected Result: 40 fruits

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub