collectionReduce

Sum amount of an item value for every item

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

This function requires Lucee. Not supported on Adobe ColdFusion.

collectionReduce Argument Reference

collection any
Required

closure function
Required

initalValue any

Examples sample code invoking the collectionReduce function


Sum amount of an item value for every item

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


Fork me on GitHub