Filter, Reduce, and Map in JavaScript: A Comprehensive Guide

ยท

3 min read

We'll be diving deep into three of the most transformative functions in JavaScript: filter, reduce, and map.

1. The map Function

What is it?

The map function is your go-to when you want to transform every item in an array without changing the original array. It creates a new array with the results of the transformation.

Syntax:

array.map(function(item) {
    // transform item
    return transformedItem;
});

Example:

  • Doubling Numbers: Let's take a simple example where we have an array of numbers, and we want to double each one:

      const numbers = [1, 2, 3, 4];
      const doubled = numbers.map(num => num * 2);
      console.log(doubled); // [2, 4, 6, 8]
    
  • Capitalizing Strings: Suppose you have an array of strings and you want to capitalize each one:

      const words = ["apple", "banana", "cherry"];
      const capitalized = words.map(word => word.toUpperCase());
      console.log(capitalized); // ["APPLE", "BANANA", "CHERRY"]
    

2. The filter Function

What is it?

The filter function comes in handy when you need to sift through an array and pick items that meet a specific condition. It creates a new array containing only those items.

Syntax:

array.filter(function(item) {
    // return true to keep the item, false otherwise
    return condition;
});

Example:

  • Finding Even Numbers: Let's continue with our numbers and filter out only the even ones:

      const numbers = [1, 2, 3, 4];
      const evens = numbers.filter(num => num % 2 === 0);
      console.log(evens); // [2, 4]
    
  • Filtering Words: Say you want to keep words in an array that have more than 5 letters:

      const words = ["apple", "banana", "cherry", "date"];
      const longWords = words.filter(word => word.length > 5);
      console.log(longWords); // ["banana"]
    

3. The reduce Function

What is it?

reduce is a versatile tool that allows you to boil down an array to a single value. This value can be a number, string, object, or even another array.

Syntax:

array.reduce(function(accumulator, currentItem) {
    // combine accumulator and currentItem
    return newValue;
}, startingValue);

Example:

  • Summing Up Numbers: Let's find the sum of our numbers:

      const numbers = [1, 2, 3, 4];
      const sum = numbers.reduce((acc, num) => acc + num, 0);
      console.log(sum); // 10
    
  • Creating an Object: Suppose you have an array of fruits and you want to create an object where each fruit is a key and its length is the value:

      const fruits = ["apple", "banana", "cherry"];
      const fruitLengths = fruits.reduce((obj, fruit) => {
          obj[fruit] = fruit.length;
          return obj;
      }, {});
      console.log(fruitLengths); // {apple: 5, banana: 6, cherry: 6}
    

The Power of Chaining

One of the beauties of these functions is that they can be chained together. For instance, you can filter an array and then map the results:

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

In Summary

filter, reduce, and map are not just mere functions. They represent a functional approach to programming in JavaScript, emphasizing immutability and declarative code. By understanding and mastering them, you're setting yourself up for cleaner, more efficient, and more maintainable code.

If you have any questions or suggestions then, feel free to reach out to me on Twitter or LinkedIn. You can find me on Twitter DivyParekh and LinkedIn at LinkedIn. I look forward to connecting with you and discussing all things!

Did you find this article valuable?

Support Divy Parekh by becoming a sponsor. Any amount is appreciated!

ย