Course contentsShow
← JavaScript
Lesson 852 of 2,72320. Array Iteration and TransformationFree lesson

Chaining map Operations

Combining multiple map calls to perform sequential transformations on array data.

Chaining map Operations

What you'll learn: You'll discover how to chain multiple map() calls together to perform step-by-step transformations on array data.

The Core Idea

Since map() returns a new array, you can immediately call map() again on that result. This lets you break complex transformations into simple, readable steps where each map() does one clear job.

Think of it like an assembly line: the first station paints items red, the second adds labels, the third packages them. Each worker passes their result to the next.

How Chaining Works

When you chain map() operations, the output array from the first map() becomes the input for the second:

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

const result = numbers
  .map(num => num * 2)      // [2, 4, 6, 8]
  .map(num => num + 10)     // [12, 14, 16, 18]
  .map(num => `$${num}`);   // ["$12", "$14", "$16", "$18"]

console.log(result); // ["$12", "$14", "$16", "$18"]

Each map() processes every element before passing the transformed array to the next map().

Why Chain Instead of Combining?

You could do everything in one map():

const result = numbers.map(num => `$${num * 2 + 10}`);

But chaining makes each transformation explicit and easier to understand, modify, or debug. Each step has a single, clear purpose.

Real-World Example

const prices = [19.99, 29.99, 9.99];

const taxedRoundedLabels = prices
  .map(price => price * 1.08)          // Add 8% tax
  .map(price => Math.round(price))     // Round to nearest dollar
  .map(price => `Price: $${price}`);   // Format as string

console.log(taxedRoundedLabels);
// ["Price: $22", "Price: $32", "Price: $11"]

Key Takeaway: Chaining map() operations lets you perform multi-step transformations by passing the result of one map() directly into the next, creating readable, modular data pipelines.