map, filter, reduce
JavaScript arrays have powerful built-in methods that transform data efficiently. The three most important are map, filter, and reduce.
Key Concept: These methods don't modify the original array. They create new arrays or values based on transformations you define.
Creates a new array by applying a function to each element.
let numbers = [1, 2, 3]; let doubled = numbers.map(n => n * 2); // [2, 4, 6]
Creates a new array with only items that pass a test.
let numbers = [1, 2, 3, 4]; let evens = numbers.filter(n => n % 2 === 0); // [2, 4]
Reduces an array to a single value (sum, product, object, etc.).
let numbers = [1, 2, 3]; let sum = numbers.reduce((total, n) => total + n, 0); // 6