Back

Array Methods (map, filter, reduce)

Array methods let you work with lists of data in a clean and powerful way. The three most important ones are map, filter, and reduce. Think of arrays like a list of items, and these methods as tools to work with that list.

const numbers = [1, 2, 3, 4, 5]

// map: transform every item
numbers.map(n => n * 2)         // [2, 4, 6, 8, 10]

// filter: keep only matching items
numbers.filter(n => n > 2)      // [3, 4, 5]

// reduce: combine into one value
numbers.reduce((a, b) => a + b) // 15

// find: get first matching item
numbers.find(n => n > 3)        // 4

// some: does any item match?
numbers.some(n => n > 4)        // true

// every: do all items match?
numbers.every(n => n > 0)       // true

Roadmap