Map, Filter and Reduce in JS

Aug 30, 2022 | Programming | Javascript | Code

Normally I don't write about programming topics in this blog, since I dedicate it more to pictures, maps and charts, but it happens that a couple of days ago I was explaining to a colleague at work some parts of an internal tool in which we use React and functional programming.

And it is true that if you haven't had experience with the higher order functions of javacript of map, filter & reduce, they might appear daunting at the beginning. But once you get used to them you never look back!. They are extremely useful in manipulating arrays and arrays of objects

So objective of this post is to explain the three of them using a simple example.

Data

Let’s create a quick array of objects with the 5 top five movies of all times according to the imdb ratings:

const top5Movies = [
    {
        "title": "The Shawshank Redemption",
        "rank": "1",
        "id": "tt0111161",
        "imdbrating": "9.3",
        "year": "1994",
        "genre": "Drama",
        "director": "Frank Darabont",
        "actors": "Tim Robbins, Morgan Freeman, Bob Gunton",
        "language": "English",
        "country": "United States"
    },
    {
        "title": "The Godfather",
        "rank": "2",
        "id": "tt0068646",
        "imdbrating": "9.2",
        "year": "1972",
        "genre": "Crime, Drama",
        "director": "Francis Ford Coppola",
        "actors": "Marlon Brando, Al Pacino, James Caan",
        "language": "English, Italian, Latin",
        "country": "United States"
    },
    {
        "title": "The Godfather: Part II",
        "rank": "3",
        "id": "tt0071562",
        "imdbrating": "9.0",
        "year": "1974",
        "genre": "Crime, Drama",
        "director": "Francis Ford Coppola, Mario Puzzo",
        "actors": "Al Pacino, Robert De Niro, Robert Duvall",
        "language": "English, Italian, Spanish, Latin, Sicilian",
        "country": "United States"
    },
    {
        "title": "Pulp Fiction",
        "rank": "4",
        "id": "tt0110912",
        "imdbrating": "8.9",
        "year": "1994",
        "genre": "Crime, Drama",
        "director": "Quentin Tarantino",
        "actors": "John Travolta, Uma Thurman, Samuel L. Jackson",
        "language": "English, Spanish, French",
        "country": "United States"
    },
    {
        "title": "The Good, the Bad and the Ugly",
        "rank": "5",
        "id": "tt0060196",
        "imdbrating": "8.8",
        "year": "1966",
        "genre": "Adveture, Western",
        "director": "Sergio Leone",
        "actors": "Clint Eastwood, Eli Wallach, Lee Van Cleef",
        "language": "Italian, English",
        "country": "Italy, Spain, West Germany"
    }
]

Map

Let’s say a friend says to you, ah great that dataset, but could you give me just a list with just the titles of those movies? In this case map is your friend :). This method loops over an array and runs a function on each element of the array, returning the modified value as a new element of a new array.

In our case we want to loop over this top5Movies array and just extract the titles:

const titles = top5Movies.map(movie => movie.title)

So with this simple statement we loop over each “movie” item (an object), and apply a simple function (with arrow notation) that returns the title (default return), and assigns them to this new titles array. If we look at the contents of this array, our titles should be there:


> titles
[
  'The Shawshank Redemption',
  'The Godfather',
  'The Godfather: Part II',
  'Pulp Fiction',
  'The Good, the Bad and the Ugly'
]

If you want to better understand the concept, think about it withour arrow notation:

top5Movies.map(
    function (movie) { 
        return movie.title 
    }
)

As you can see there you are applying a function to each iteration and returning the value.

Please also note that the map method will always create a new array without mutating the original one.

Filter

Now let’s say another friend asks you if you can give him a a copy of this array of objects, but including only those in which Spanish is spoken during the movie. This where the filter operation can be quite useful. This method iterates over the source array, but instead of returning a modified copy of your array element, just test the codition defined in the function an returns a copy of the original element if the condition is met.

So for this request will check if the field “language” includes the string “Spanish”:

const topMoviesWithSpanish = top5Movies.filter(
    movie => movie.language.includes("Spanish"))

The new created topMoviesWithSpanish array will only include:

[
    {
        "title": "The Godfather: Part II",
        "rank": "3",
        "id": "tt0071562",
        "imdbrating": "9.0",
        "year": "1974",
        "genre": "Crime, Drama",
        "director": "Francis Ford Coppola, Mario Puzzo",
        "actors": "Al Pacino, Robert De Niro, Robert Duvall",
        "language": "English, Italian, Spanish, Latin, Sicilian",
        "country": "United States"
    },
    {
        "title": "Pulp Fiction",
        "rank": "4",
        "id": "tt0110912",
        "imdbrating": "8.9",
        "year": "1994",
        "genre": "Crime, Drama",
        "director": "Quentin Tarantino",
        "actors": "John Travolta, Uma Thurman, Samuel L. Jackson",
        "language": "English, Spanish, French",
        "country": "United States"
    }
]

Since those are the movies that include “Spanish” in the language field. As it was the case with map, filter will create a new copy of the array so the original is not mutated.

Reduce

Finally another friend asks you if it is possible to give the average of the ratings of those top 5 movies. So you say sure man, you take the map function you already learned and say to him, here are the ratings:

> top5Movies.map(
    movie => parseFloat(movie.imdbrating))
[ 9.3, 9.2, 9, 8.9, 8.8 ]

And your friend says well thank you, but what I mentioned is that want the “average” of those.
You start to think about it 🤔, and reliase you need to iterate over each rating but instead of changing it, you need to sum it to the next one and so on, and once you have the final sum, you can divide it by the number of ratings.
This process of “aggregating” values is exactly what the reduce method, it iterates over each element and applies a function you define with the item and the aggegator.
Lets take those ratings and let’s aggregate (sum) them together:

> [ 9.3, 9.2, 9, 8.9, 8.8 ].reduce(
    (aggregator, rating) => (aggregator + rating))
45.2

As you can observe your ratings array was “reduced” to a single value that’s the sum of the ratings.

Using this principle we can chain our map function extracting the rating with this reduce one, and by diving by the number of items we can give the average answer to our friend:

> top5Movies.map(
    movie => parseFloat(movie.imdbrating)).reduce(
      (aggregator, rating) => (
        aggregator + rating)) / top5Movies.length;
9.040000000000001

Now one of the most powerful features of this reduce high order function is that it allows you to to create an object from an array.

In that case the reduce function takes two arguments: a starting value and a function. The starting value is the initial value of the accumulator, which is the object that will be created. The function is used to iterate over the array and add each element to the accumulator. The function takes two arguments: the accumulator and the current element.

So let’s go back to our movies example. Let’s say we want to convert that top5Movies array into an object, keyed by the imdb id, so it’s easier to get a movie based on this id.

So let’s do it two steps. First let’s create the template that will use for write our reduce function:

top5Movies.reduce((acc, item) => { return acc}, {})

Obviously this return an empty object, because it’s iterating over our top5movies but returning always the empty accumulator (acc) that we initialized at the end of this reduce function ({}). But if now we just add:

top5Movies.reduce((acc, item) => { acc[item.id] = item; return acc}, {})

Then now the accumulator will gradually add the movie keyed by its id and at the end we get what we want:

{
  tt0111161: {
    title: 'The Shawshank Redemption',
    rank: '1',
    id: 'tt0111161',
    imdbrating: '9.3',
    year: '1994',
    genre: 'Drama',
    director: 'Frank Darabont',
    actors: 'Tim Robbins, Morgan Freeman, Bob Gunton',
    language: 'English',
    country: 'United States'
  },
  tt0068646: {
    title: 'The Godfather',
    rank: '2',
  ....

And that’s it!. I hope this post will be useful to understand better the map, filter & reduce high order functions in Javascript.