Course contentsShow
← JavaScript
Lesson 1782 of 2,72337. Functional Programming PatternsFree lesson

Composing with Array Methods

Apply composition to chain map, filter, and other array methods for readable data transformation pipelines.

Composing with Array Methods

You'll learn how to chain map, filter, reduce, and other array methods into elegant pipelines that transform data step-by-step.

The Core Idea

Instead of nesting function calls or creating temporary variables, you can compose transformations directly on arrays using method chaining. Each method returns a new array (or value), letting you build readable, declarative pipelines that express what you want, not how to get it.

Think of it like an assembly line: raw materials (your data) pass through multiple stations (array methods), each performing one specific transformation, until you get the finished product.

Building Data Pipelines

const users = [
  { name: 'Alice', age: 28, active: true },
  { name: 'Bob', age: 34, active: false },
  { name: 'Carol', age: 22, active: true }
];

// Pipeline: filter → map → reduce
const totalActiveAge = users
  .filter(user => user.active)           // Keep only active users
  .map(user => user.age)                 // Extract ages
  .reduce((sum, age) => sum + age, 0);   // Sum them up

console.log(totalActiveAge); // 50

Each method feeds its result into the next. This is composition in action—small, focused transformations stacked together.

Why This Works

Array methods like map and filter are unary-friendly: they take a single array and return a new one, making them perfect for chaining. The data flows naturally from left to right (or top to bottom), making your intent crystal-clear.

// Readable transformation pipeline
const shoutingActiveNames = users
  .filter(u => u.active)
  .map(u => u.name)
  .map(name => name.toUpperCase());

Key Takeaway: Chaining array methods creates clean, readable composition pipelines where each step performs one transformation, letting you build complex data processing without nesting or intermediate variables.