Instead of writing a manual for loop to transform, filter, or summarize an array, ColdFusion provides functional-style methods that take a closure (an inline function) and apply it to every element for you.
These read closer to describing what you want ("give me only the ones that match") than how to get it (looping, checking, and building a new array by hand).
Learning Objectives
After completing this lesson, you'll be able to:
- Transform every element with arrayMap.
- Keep only matching elements with arrayFilter.
- Reduce an array to a single value with arrayReduce.
- Run a function for every element with arrayEach.
- Check whether all, or any, elements match a condition with arrayEvery and arraySome.
arrayMap — Transform Every Element
Returns a new array where each element is the result of running the callback on the original element. The original array isn't changed.
The callback receives (value, index, array) — most of the time you'll only need the first parameter, value.
arrayFilter — Keep Only Matching Elements
Returns a new array containing only the elements where the callback returned true.
arrayReduce — Combine Into a Single Value
Runs the callback once per element, carrying an accumulated result forward each time, and returns the final value. The callback receives the running result first, then the current element.
The third argument to arrayReduce (0 here) is the starting value for the accumulator — without it, the first element itself is used as the starting point.
arrayEach — Run Something for Every Element
Unlike map, filter, and reduce, arrayEach doesn't build or return anything — it's for side effects, like writing output or logging, once per element.
This is functionally similar to a for...in loop — use whichever reads more naturally in context.
arrayEvery — Do All Elements Match?
arraySome — Does At Least One Element Match?
Chaining Operations
Since arrayFilter and arrayMap both return new arrays, they can be chained — filter down to what you need, then transform what's left.
Real-World Example: Order Summary
Common Beginner Mistakes
Forgetting arrayFilter's callback must return a Boolean
Returning a value like item.rating (a number) instead of item.rating >= 3 (a true/false comparison) doesn't filter the way you'd expect — CFML will coerce it, but the result is confusing. Always return an actual comparison.
Forgetting arrayReduce's starting value
Without the third argument, the first element becomes the starting accumulator instead of running through the callback itself — usually not what you want when summing prices starting from 0.
Using arrayEach when you actually needed arrayMap
arrayEach doesn't build a new array — if you need the transformed values afterward, use arrayMap instead.
Best Practices
- Use arrayMap and arrayFilter over a manual for loop when you're building a new array — it communicates intent more clearly.
- Use arrayReduce for totals, counts, or building up a single combined result.
- Always pass an explicit starting value to arrayReduce for numeric accumulation, even when 0 seems obvious.
- Use arrayEach only for side effects (output, logging) — not for building a result, since it doesn't return one.
Interview Questions
What's the difference between arrayMap and arrayFilter?
arrayMap transforms every element and returns a same-length array of the results. arrayFilter keeps only the elements where the callback returns true, so the result can be shorter.
What does arrayReduce's callback receive, and in what order?
The running accumulated result first, then the current element (plus optionally the index and the original array).
How is arrayEach different from arrayMap?
arrayEach runs a function for every element but returns nothing (void) — it's for side effects. arrayMap returns a new array of transformed values.
What's the difference between arrayEvery and arraySome?
arrayEvery returns true only if the callback is true for every element. arraySome returns true if it's true for at least one element.
Summary
In this lesson, you learned ColdFusion's functional array operations: arrayMap to transform, arrayFilter to keep matching elements, arrayReduce to combine into a single value, arrayEach for side effects, and arrayEvery / arraySome for all-or-any condition checks.
What's Next?
The next lesson covers array conversion and aggregation functions — arraySum, arrayAvg, arrayToList, listToArray, and arrayToStruct.