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?

  1. More readable - Intent is clear
  2. Less error-prone - No off-by-one errors
  3. Chainable - Combine operations fluently
  4. 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.