Array Methods — *The Heart of Functional Style*
Array Methods — *The Heart of Functional Style*
🎯 What you'll be able to do after this lesson
After completing this lesson, you'll be confident doing these 3 things:
- ▸✅ Chaining map · filter · reduce · find · some · every
- ▸✅ Understanding that sort mutates the original — use toSorted (ES2023) for immutability
- ▸✅ Using flat · flatMap · Array.from
Keep the learning goals as a checklist — close the lesson once you can answer all of them.
The 5 Essential Methods
The One-Line Summary
Master just the functional methods of JS arrays — map·filter·reduce·find·some/every — and 90% of loops disappear. The beginning of functional thinking.
map — Transform Each Element
Old code: for loop + push. Modern: .map() in one line.
filter — Keep Only What Passes
reduce — Fold Down to One
reduce is the most powerful method — it can even replicate map and filter. Confusing at first, but powerful once it clicks.
find — First Match
filter returns all matches, find returns only the first one.
some / every — Truth Checks
if (array.some(...)) expresses intent more clearly than if (array.find(...)).
Chaining — Composing It Together
The filter → map → reduce pattern mirrors SQL's WHERE → SELECT → SUM. The intent reads linearly.
More Frequently Used Methods
Common Pitfalls
1. Mutating vs. returning a new array:
- ▸
sort·reverse·push·pop·splice— mutate original ⚠️ - ▸
map·filter·slice·concat— return a new array ✅
2. forEach vs map:
forEach is for side effects only, map creates a new array. Use the right one for the job.
Quick Summary
- ▸
map·filter·reduce= the functional trio - ▸Chain them to compose logic
- ▸Mutating vs. non-mutating distinction is essential
- ▸You'll rarely ever need a
forloop
find · some · every · flatMap — 5 More Methods
find — Just the First Match
Unlike filter, it stops immediately at the first match. The standard for single-record lookups.
findIndex — Just the Position Index
some / every — Boolean Return
Frequently used for input validation and permission checks:
flat / flatMap — Flattening Nested Arrays
Array.from — Array-like / Iterable → Array
Chaining Patterns — *The Go-To Expression in Vibe Coding*
Transform, Filter, and Aggregate in One Go
10 lines of for loops → a 4-line chain. Nearly the standard pattern for AI-generated code.
Sort Then Take Top N
Note: sort() mutates the original array. For immutability, use [...users].sort(...) or users.toSorted(...) (ES2023).
Group and Transform
In ES2024, Object.groupBy(orders, o => o.status) does it in a single line.
Common Pitfalls
1. Forgetting the initial value in reduce
Omitting the initial value on an empty array throws an error. Always specify an initial value.
2. Causing side effects inside map
map is meant to build a new array. If you're not using the result, forEach is the right choice.
🤖 Try asking AI like this
- ▸"Rewrite this for loop as a filter/map/reduce chain"
- ▸"From this code, sum the amount field for orders where status='paid' and print the result"
- ▸"Refactor this sort call to use toSorted so it doesn't mutate the original"