map() , filter() , reduce() in javascript

hammad razahammad raza
3 min read

Hello everyone! Today, as someone new to tech blogging, I want to share my beginner-friendly journey into three powerful JavaScript array methods: map, filter, and reduce. These methods help you work with arrays in a clean, modern way. I’ll include simple code snippets, real-world examples, and show you how these are used in actual websites.

What Are map, filter, and reduce?

  • map: Transforms each element in an array and returns a new array.

  • filter: Selects elements from an array that meet a certain condition.

  • reduce: Combines all elements in an array into a single value (like a sum or an object).

1. map(): Transforming Arrays

const numbers = [1, 2, 3, 4];
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6, 8]

Simple Example: Doubling Numbers

const numbers = [1, 2, 3, 4];
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6, 8]

Real-World Example: Extracting Data from API Results

Suppose you have an array of user objects from an API, and you want only their names and emails:

const users = [
  { name: "Alice", email: "alice@email.com", age: 25 },
  { name: "Bob", email: "bob@email.com", age: 30 },
  { name: "Carol", email: "carol@email.com", age: 22 }
];

const userContacts = users.map(user => ({
  name: user.name,
  email: user.email
}));

console.log(userContacts);
// [ { name: "Alice", email: "alice@email.com" }, ... ]

This is super useful for displaying user lists or sending newsletters

2. filter(): Selecting Items

Syntax

const filteredArray = array.filter((currentValue, index, array) => {
  // return true to keep the element
});

Simple Example: Filtering Even Numbers

const numbers = [1, 2, 3, 4, 5, 6];
const evens = numbers.filter(num => num % 2 === 0);
console.log(evens); // [2, 4, 6]

Real-World Example: Filtering Active Users

Suppose you want to show only users who are active and above 30 years old:

const users = [
  { name: "Alice", age: 25, active: true },
  { name: "Bob", age: 30, active: false },
  { name: "Carol", age: 35, active: true }
];

const activeAdults = users.filter(user => user.active && user.age > 30);
console.log(activeAdults);
// [ { name: "Carol", age: 35, active: true } ]

3. reduce(): Summarizing Data

Syntax

const result = array.reduce((accumulator, currentValue, index, array) => {
  // return new accumulator value
}, initialValue);

Simple Example: Summing Numbers

const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((total, num) => total + num, 0);
console.log(sum); // 10

Real-World Example: Counting Items by Category

Suppose you want to count how many users are in each city:

const users = [
  { name: "Alice", city: "New York" },
  { name: "Bob", city: "London" },
  { name: "Carol", city: "New York" }
];

const cityCounts = users.reduce((acc, user) => {
  acc[user.city] = (acc[user.city] || 0) + 1;
  return acc;
}, {});

console.log(cityCounts);
// { "New York": 2, "London": 1 }

Why Use These Methods?

  • Cleaner code: Less boilerplate than loops.

  • Immutability: They don’t change the original array.

  • Functional style: More readable and easier to debug.

Conclusion

Learning map, filter, and reduce opens up a world of possibilities in JavaScript. Whether you’re building a shopping cart, a dashboard, or a social feed, these methods help you write code that’s clean, efficient, and easy to maintain.

Happy coding!

0
Subscribe to my newsletter

Read articles from hammad raza directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

hammad raza
hammad raza