Sitemap

Filter? Map!? Reduce!?!?

4 min readMar 19, 2018

--

So lately I’ve been studying code with SuncoastJS. And as of last weekend, I had a chance to get really, really familiar with JavaScript array methods, and most notably Filter, Map, and Reduce.

Long story short, they’re awesome!

The only downside is that at first they’re way difficult to understand, so via this blog post my goal is to break these methods down as clearly as possible. To make it less painful, we’re going to start with Map. Before that, however, let’s make some things clear:

1: All of these only operate on arrays. The methods only exist on JavaScript arrays.

2: All of these methods operate on the values of an array and return a new array. They DO NOT mutate the input array.

3: Every single one of these is shorthand for something you can already do in JavaScript.

With that out of the way, let’s get started!

Array.prototype.map()

What it must take in: An array

What it gives back: A new array with every value changed somehow.

Array.map() is great because it gives you a shorthand way to change each value of an array. When called, you have the option to provide a function where the first parameter is the value of the array that you’re operating on, along with optional parameters to get the index of that element, and one to get the entire array. After the function you have an option to decide the value of this, but we don’t need to worry about that right now.

Inside of the array map, you have an option to provide a function, and that function’s return value determines what you get back. So if I had the code snippet:

let arr = [1,2,3,4,5];let newArr = arr.map((num) => {
return num * 2;
});
console.log(newArr);

I would expect to get:

[2,4,6,8,10]

See? Array.map() ran a function on every single value of the array, and then spat that back out into a new array for me.

That code snippet would essentially be equivalent to:

let arr = [1,2,3,4,5];
let newArr = [];
for(let I = 0; I < arr.length; i++) {
let newVal = arr[i] * 2;
newArr.push(newVal);
}
console.log(newArr);

As you see, we still get the same result: [2,4,6,8,10]

Array.prototype.filter()

What it must take in: An Array

What it gives back: A new array with all values that pass a given test.

Array.filter() gives you a nice shorthand to pull out information from an array. Similar to map, you need to call a function with a given element. Of course, the index and array parameters are still available as well. You can think of filter as nearly identical in terms of syntax, just it gives you back something else.

For example:

const arr = [1,2,3,4,5];const newArr = arr.filter((elem) => {
return num < 3;
});
console.log(newArr);

I would expect to get: [1,2]

Array.filter() looked at the values that were coming in, and then only returned them if they passed the specified test!

Get Christopher Tiller’s stories in your inbox

Join Medium for free to get updates from this writer.

It is equivalent to the following longhand:

const arr = [1,2,3,4,5];
let newArr = [];
for(let I = 0; I < arr.length; i++) {
if(arr[i] < 3) {
newArr.push(arr[i]);
}
}
console.log(newArr);

and as before, we get: [1,2]

Array.prototype.reduce()

Strap in folks, this is a fun one!

So while Array.map() had the goal of changing every single value of an array, and Array.filter() had the goal of only getting values of an array that pass a certain test, reduce has the goal of combining everything down into a single value, or “reducing” an entire array to a single value.

Similar to the prior two methods, it does operate on an array, and it does take in a function parameter. The difference begins with the internal function. Inside of that function, we take two values, normally called “accumulator” and “value”, respectively. To break it down, “accumulator” is the combination of every single element so far, while value is just the current element. As reduce runs, it takes the return from the function and makes that the new accumulator for every single element.

Eyes starting to cross? Don’t worry, here’s an example!

const arr = [1,2,3,4,5];let combinedVal = arr.reduce((acc, val) => {
return acc + val;
});
console.log(combinedVal);

and we’ll get: 15

So what the heck just happened there? Let’s break it down step-by-step.

For the first run of arr.reduce, we essentially had:

return 1 + 2;

1 + 2 = 3, so that becomes our new accumulator for the next run.

On the second run of arr.reduce, we have:

return 3 + 3;

3 + 3 = 6, so that becomes our new accumulator for the next run:

return 6 + 4;

Alright, that’s fine, that gives us 10! And on the final run we have:

return 10 + 5;

See what happened? It took the value, returned it, and made that value the new accumulator on each run, effectively “summing” all of the elements together.

For those of you still curious, that would be equivalent to:

let arr = [1,2,3,4,5];
let sum = 0;
for(let I = 0; I < arr.length; i++) {
sum += arr[i];
}
console.log(sum);

And that’s it!

So, to recap:

-Map is for when you want to change every value of the array somehow.

-Filter is for when you want to get a few values that pass a test out of an array.

-Reduce is for when you want to somehow combine all values of the array.

-All three methods create a brand new array.

-All three methods take in a function.

And that’s it! Hope it was helpful. Let me know if you have any questions in the comments!

--

--