42

Map, Filter and Reduce – Animated - JavaScript Teacher - Medium

 5 years ago
source link: https://medium.com/@js_tut/map-filter-and-reduce-animated-7fe391a35a47?source=friends_link&%3Bsk=0100c8ecaf13fe61fe1adc94173703dc&%3Brv=75
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

You have 2 free member-only stories left this month.

Map, Filter and Reduce – Animated

Map, filter and reduce have been around for a long time. They are often seen as part of Functional Programming style.

Here’s a list of my best web development tutorials.

Complete CSS flex tutorial on Hashnode.

Ultimate CSS grid tutorial on Hashnode.

Higher-order functions .map, .filter & .reduce on Hashnode.

Follow me @Twitter, Instagram & fb to never miss premium articles.

We often commit complex ideas to memory by visualizing them. There aren’t many coding articles using visuals as starting point for learning. And yet, visualization plays important role in education. This also applies to coding.

Even after working with map, filter and reduce for years I still often find myself asking: Is a copy of the original array made? Is reference to the original array modified? I made this tutorial to put an end to these questions.

My hope is that once it is seen visually – it will be easier to memorize.

Array.map map all values to an expression.

Image for post
Image for post
Array.map(): apply “value + 1” to a set of 7 numbers [1, 2, 3, 4, 5, 6, 7]

1] Expression value + 1 is applied to every item in the original array.

2] .map() returns a modifiedcopyof the array leaving original untouched.

3] Result: [2,3,4,5,6,7,8] (a copy of the original array is created.)

Array.filter keep all that match a condition.

Image for post
Image for post

NOTE: there is a small mistake in the animation. It should return [6,7], not [6,7,8]. I’ll fix it shortly. . .

1] function value > 5 is applied to every item in the original array.

2] .filter() returns a modified copyof the array – the original is still available!

3] Result: [6,7,8] (only values that passed the condition.)

Array.reduce – reduce all items to a single value.

A common use for a reducer is combining prices of all items in shopping cart.

What makes reduce unique is that it uses an accumulator.

Accumulator is required to be set to a starting value. Here it is 0.

Image for post
Image for post

1 ] Reducer function F takes value and accumulator.

2 ] In this example .reduce(v, a) returns the sum of all values.

3 ] Result: 28 (the sum of all numbers in the original array)

Conclusion

Of course, these higher order functions can (and should) be used to solve a wide variety of different problems.

Map, Filter and Reduce can also be used with arrays of objects. They are not limited to working with numeric values.

Follow me @Twitter, Instagram & fb to never miss premium articles.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK