Map/Filter/Reduce Crash Course
Chris Achard

Chris Achard @chrisachard

About: I'm trying to teach everything I know at chrisachard.com Instructor at egghead.io Mostly, I use JS, React, Rails, and Node

Location:
USA
Joined:
Aug 14, 2019

Map/Filter/Reduce Crash Course

Publish Date: Sep 17 '19
622 25

This was originally posted as a Twitter thread: https://twitter.com/chrisachard/status/1173750491458789378

Have you heard about map, filter, and reduce, but never really understood them?

Here's a 🔥 map().filter().reduce() 🔥 crash course for you!

1.

map, filter and reduce can all operate independently, or be chained together

They operate on an array and transform it

Map, Filter, Reduce

2.

filter takes the array and returns a new array that only contains the elements that match some condition

Filter

3.

It loops over the elements, passing each one to a callback function

You can return true to include that element in the new array, or false to exclude it

Filter loops over the elements

4.

Think of map like an element transform function

It loops over an array, and you can return a new element for each spot in the array

Map

5.

This lets you transform each element into something new (or keep it the same)

Types don't have to be the same: can return objects, string, numbers - anything!

Map to transform elements

6.

reduce loops over an array and let's you "collect" the elements into something else (by running a function)

That "something else" is specified by you as the second argument

This way, you can "collapse" (reduce) the array into a new array, an object, number, etc.

Reduce

7.

During each loop of reduce, you can get result of the last loop, and the next element in the array

Change the result, then return it for the next loop iteration

When you're done, you have the completed collection

Reduce loop

8.

The callback functions to map, filter and reduce all can also get the current index and the entire original array if you need them

Map, filter, reduce

9.

All together now:

Chain filter with map to first remove elements you don't care about, and then transform them

Or,

Chain filter with reduce to filter the list first, then transform it into something else

Chain

10.

So why are map, filter and reduce useful?

  • don't have to manually loop over array
  • chain together for short, straightforward array transformations
  • can reuse callback functions and compose them together

useful composition

Code Links

Here are some interactive code samples to play with: https://chrisachard.com/examples/map-filter-reduce

I realize this can be confusing!

Tweet at me or DM if I can help out 🙌

 

Like this crash course?
Find more on twitter: @chrisachard
and in my my newsletter 📬

Thanks for reading!

Comments 25 total

  • Daniel Worsnup
    Daniel WorsnupSep 17, 2019

    Great summary! I wrote about reduce recently as well: dev.to/worsnupd/everything-you-nee... It's cool to see how a lot of Array prototype methods are just special cases of reduce.

    • Chris Achard
      Chris AchardSep 17, 2019

      Yep! reduce is super powerful (which is why it can be so confusing!)

  • Anton Ödman
    Anton ÖdmanSep 17, 2019

    I love your articles!

    So concise, well written and easy to follow, great work! 👏

    • Chris Achard
      Chris AchardSep 17, 2019

      Thanks! It's fun to try to break something down to just a few points. I always end up learning a ton myself while writing them 😃

  • Keith Brewster
    Keith BrewsterSep 17, 2019

    This is a great breakdown, another big benefit (which you mentioned near the beginning) is that it returns you a new instance of an array instead of mutating the original one.

    • Chris Achard
      Chris AchardSep 17, 2019

      Yes, definitely - I didn't have the space to really expand on that (maybe a part 2!), but these three really shine when you want to not mutate data.

  • Jasterix
    JasterixSep 19, 2019

    This is such a great summary. I'm a bit late to using reduce, but as I use it more, I'm realizing how powerful it is

    • Chris Achard
      Chris AchardSep 19, 2019

      Thanks! Glad it was helpful.

      Exactly: reduce is something that I thought wasn't great, UNTIL I started just using it. Now I use it all the time! It's really great for condensing a lot of logic down into just a few lines.

  • Pedro Pimienta M.
    Pedro Pimienta M.Sep 19, 2019

    Great article, good expliation and the images help to much.

  • Valerio
    ValerioSep 20, 2019

    Thank you, that's a great overview!

  • Feralamillo
    FeralamilloSep 23, 2019

    Very nice article Chris!! It's great how you have summarized these 3 methods that can make it easily for developers.

  • Ryota Murakami
    Ryota MurakamiSep 23, 2019

    Thank you for write it!!
    I still confuse about Array.prototype.splice() 😅

    • Chris Achard
      Chris AchardSep 23, 2019

      Yeah, splice is confusing :) Maybe I'll try to do a writeup on that in the future!

  • Raghavan alias Saravanan Muthu
    Raghavan alias Saravanan MuthuSep 27, 2019

    Awesome post Chris. Loved the way you presented. looking forward to the other posts of you! :)

  • Manuel Blanco
    Manuel BlancoOct 4, 2019

    Great explanation! Hope to keep seeing posts like this in the future :)

  • Ebrahim Batran
    Ebrahim BatranOct 10, 2019

    I'm forwarding this to anyone who's still confused. It's the best!

  • Shannon Crabill
    Shannon CrabillFeb 20, 2020

    I keep forgetting how map, etc works and have referred to this resource at least 5 times already. It is very helpful!

  • iftikhar hussain
    iftikhar hussainApr 11, 2020

    Hey, A great knowledgeable post, Thanks
    Can you please tell me the name of your editor font ? i really like that

    • Chris Achard
      Chris AchardApr 11, 2020

      Glad you liked it!

      It's the "Menlo" font :)

  • kasra6
    kasra6Jul 21, 2020

    Thank you for your clear voice and explanation. Map and filter was easy to understand at least at this stage, but I couldn't digest reduce. like what is the previous loop and next element!
    Anyway this article was really useful. Thank you

  • YoramGondwe
    YoramGondweNov 19, 2020

    Thank the explanation is really clear

  • Let's Code
    Let's CodeJan 22, 2021

    Hi Chris, going to include the youtube channel I just created to help the community as well if you do not mind. I am trying to release one vid a week. Thank you and this article is a great crash course indeed. I can see enormous time and effort you put in.

    youtube.com/channel/UCFIwa5Eqf4kN1...

  • Tu Nguyen
    Tu NguyenNov 20, 2022

    It is very visual, I will try it in Quokka to see the run-time code and understand this :) love this post.

Add comment