Array Methods
Array methods let you transform data declaratively. Instead of writing loops,
you describe what you want to happen.
The Big Three
map - Transform each element
const numbers = [1, 2, 3]
const doubled = numbers.map((n) => n * 2)
// [2, 4, 6]
filter - Keep elements that pass a test
const numbers = [1, 2, 3, 4, 5]
const evens = numbers.filter((n) => n % 2 === 0)
// [2, 4]
reduce - Accumulate into a single value
const numbers = [1, 2, 3, 4]
const sum = numbers.reduce((acc, n) => acc + n, 0)
// 10
Why Use Array Methods?
- More readable - Intent is clear
- Less error-prone - No off-by-one errors
- Chainable - Combine operations fluently
- Type-safe - TypeScript infers types through the chain
const result = products
.filter((p) => p.inStock)
.map((p) => p.price)
.reduce((sum, price) => sum + price, 0)
These methods don't mutate the original arrayβthey return new arrays. This
makes code easier to reason about.
In this exercise, you'll master these essential array methods.