Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / JavaScript Tutorial / Chapter 3: Data Structures & Modern ES6+
JAVASCRIPT ⏱ 14 min read

Modern Array Iterators (map, filter, reduce)

Functional array methods are the core of modern JavaScript programming. They allow you to transform, filter, and summarize data with elegant, declarative code.

What is it?

Higher-order array methods accept a callback function to iterate over elements without writing manual `for` loops. • `.map()`: Transforms every element into a new array. • `.filter()`: Returns a subset of elements passing a condition. • `.reduce()`: Reduces an array into a single accumulated value (sum, tally, object).

Why is it used?

Declarative, immutable, and easy to chain. Essential for React, Vue, and modern frontend data pipelines.

Detailed Explanation

How They Work: 1. `map`: `[1, 2, 3].map(x => x * 2)` -> `[2, 4, 6]`. Output length equals input length. 2. `filter`: `[10, 25, 5].filter(x => x >= 10)` -> `[10, 25]`. Output length is less than or equal to input. 3. `reduce`: `[10, 20, 30].reduce((acc, curr) => acc + curr, 0)` -> `60`. 4. Method Chaining: You can chain `.filter().map()` to filter and format in a single statement.

Syntax Structure

Syntax Blueprint
const transformed = arr.map(item => item * 2);
const filtered = arr.filter(item => item.isActive);
const total = arr.reduce((acc, item) => acc + item.price, 0);

Code Example

JAVASCRIPT Code
const courseCatalog = [
  { id: 1, title: "Advanced Python", category: "Software", price: 2500, active: true },
  { id: 2, title: "Data Analytics", category: "Data Science", price: 2500, active: true },
  { id: 3, title: "Advanced Excel", category: "Data Science", price: 2500, active: true },
  { id: 4, title: "Legacy Pascal", category: "Archived", price: 1000, active: false }
];

// 1. FILTER: Only active Software/Data Science courses
const activeCourses = courseCatalog.filter(c => c.active);

// 2. MAP: Extract just course titles formatted
const courseTitles = activeCourses.map(c => `• ${c.title} (₹${c.price})`);
console.log("Active Course Titles:");
courseTitles.forEach(t => console.log(t));

// 3. REDUCE: Calculate total revenue if a student buys all active courses
const totalCatalogValue = activeCourses.reduce((sum, course) => sum + course.price, 0);
console.log(`Total Value of Catalog: ₹${totalCatalogValue}`);

Explanation of the Code

• `.filter(c => c.active)` removes the archived course. • `.map(...)` transforms objects into formatted presentation strings. • `.reduce(...)` sums all prices starting from accumulator 0.

Expected Output

Active Course Titles: • Advanced Python (₹2500) • Data Analytics (₹2500) • Advanced Excel (₹2500) Total Value of Catalog: ₹7500

Real-World Application

Used in React components to render UI cards (`courses.map(c => <CourseCard ... />)`), filter search results, and sum shopping cart totals.

Common Mistakes to Avoid

• Using `.map()` when you don't return anything (use `.forEach()` if you just want side effects like logging). • Forgetting the initial accumulator value in `.reduce(..., 0)`.

Quick Practice Challenge

Given `[12, 5, 8, 130, 44]`, filter for numbers greater than 10 and multiply each by 2.

Key Takeaways

• `.map()` creates a new array by transforming each element. • `.filter()` creates a new array with elements that pass a test. • `.reduce()` boils down an array into a single accumulated result.