Member-only story
How to Use JavaScript’s .map, .filter, and .reduce
How to use these three useful JavaScript functions
If you’re reading this, you’ve probably heard somewhere about the JavaScript functions .map()
, filter()
, and .reduce()
. Don’t worry — you’re not behind the curve on learning what they are. You’re ahead of the game if you’re reading this article, and so let's get you caught up to speed as quickly as we can.
JavaScript has created a few iterators that are based off the list objects or arrays. These iterators are helpful in providing efficient ways to do things you normally would do with a for-loop.
All of my examples are written using es6 syntax, and so if you don’t know about es6, you should check out this article.
I’m going to explain why you would use these functions and how to use them. Let’s check it out!
.map()
Are you looking to create a new array with the same number of elements in it as another array? If so, you want to use .map()
. It can also be a useful way to do plain old iterations over a list.
Let's see how to use it:
const array1 = [ 1, 2, 3, 4, 5 ];// map accepts a function. Using es6 syntax...
const array2 = array1.map(num => num + 2)
console.log(array2)…