DEV Community

.map(), .filter(), and .reduce()

Andy Pickle on July 16, 2019

Originally posted on my blog For the last several weeks I've been applying for jobs. Sadly, the startup I was working at just didn't work out. I...
Collapse
 
mongopark profile image
Ola' John Ajiboye • Edited

Hey,
Great job from your end. I prefer functional programming.Maybe because I have a Maths background and I dread for loops😉

One advice to make it easier for you to remember and also make your code more readable.
For example your filter should look something like this:


const evenNumbers = numbers.filter(number => number %2 === 0)

Just naming your variable correctly reduces the confusion.

One, you know the filter is returning even numbers. Also, it will quickly stick that you are just iterating over numbers and checking if each number is even. That way that variable wouldn't look made up. It will make perfect sense.

Best wishes.

Collapse
 
gfoxavila profile image
Andres Avila Wille

Excellent contribution! I actually like to have this in hand, to remind me of that kind of stuff.

cheatography.com/costemaxime/cheat...

Collapse
 
therealkevinard profile image
Kevin Ard

Very good input. I have a single critique, though, and this is ONLY because you're prepping: with reduce. Me, as a hiring person, I question deeper when candidates don't provide the third init value.

I know it's arguably pointless, especially when implicit inits come into play, but that's how it is. And you'll come across others who think the same.

To reiterate, this is gold. But those little things... A lot of times, hiring people will grab onto them and dig deeper. (Probably not what you want for a smooth-as-butter interview lol)

Collapse
 
pickleat profile image
Andy Pickle

Interesting, I’d love to learn more, could you expound with an example?

Collapse
 
therealkevinard profile image
Kevin Ard

I was on mobile last night and I've been busy today, so I honestly didn't expect to have a reply. ...but whaddaya know, I JUST NOW used a reduce lol.

The init value - technically - protects you from a TypeError if you encounter an empty array. init, if not provided, is implicitly taken from arr[0]. If !arr[0]... Muerte.

Here, I'm writing a cypress e2e test that finds all the card rows in the page and guarantees the contained cards are the same height. To do so, I sum the heights of all the cards, divide that by cards.length, and compare the final avg to cards[0]. (a variance inside will break the cards[0]===avg, I hope :D)

Not such a problem with Cypress, but it could be possible that there are no cards in the row.

Most uses of reduce aren't for summing, though. Its return - more often than not - is a complex object, rather than a simple value. In these cases:

  1. arr[0] can't properly init the product, because product and items have very different structures.
  2. providing an init that models the outcome helps self-document the code (other devs see in the call an idea of what to expect), and it save you a ton of checks internally.
it.only('cards within a row are equal-height', () => {
    cy.get('[data-t="card-row"]')
        .each(row => {
            let heights = []
            cy.wrap(row)
                .find('div.card')
                .each(card => heights.push(Math.floor(card.innerHeight())))
                .then(() => {
                    let avgHeight = heights.reduce((prev, curr) => prev + curr, 0) / heights.length
                    expect(heights[0]).to.eq(avgHeight)
                })
        })
})
Thread Thread
 
pickleat profile image
Andy Pickle

Very cool! Thanks for sharing this example.

Collapse
 
astromium profile image
Ayoub Belouadah

I still dont get the .reduce() 😔

Collapse
 
mateiadrielrafael profile image
Matei Adriel • Edited

Since you understand map but don't understand reduce, implementing map with reduce might help:

const map = (arr, func) => arr.reduce((acc, curr) => [...acc, func(curr)], [])

Edit: here's the same for filter:

const filter = (arr, func) => arr.reduce((acc, curr) => func(curr) ? [...acc, curr] : acc, [])
Collapse
 
pickleat profile image
Andy Pickle

That’s ok! It took me a long time to get it. Reduce is useful because you get a single value returned from an array of values. For example, if you have an array and you need to total of all the numbers.

The accumulator is just the total up to that point and the currentValue is just the value at whatever index you are currently at. So if you are adding, it’ll be the total + the value and then repeat until it’s gone through every number in the array and return the value of the last operation. Hope this helps! Try playing with the MDN examples they may help too!

Collapse
 
unodwicho profile image
UnoDwicho

Hi Andy, I'm a newbie to JS so first of all, thanks for your contribution. Very useful stuff in here!
A common problem I encounter when reading documentation on JS is figuring out what words are actual keywords and what are the made up stuff.
Do you have any other example like the map() method?

Collapse
 
pickleat profile image
Andy Pickle

Hey! I’ve seen this but haven’t had time to think of another example. Check the MDN docs linked in the article and you’ll find more!

Collapse
 
pedroapfilho profile image
Pedro Filho

Hey! Nice article!

I have a repository with some examples, if anyone is interested, check it out: github.com/pedroapfilho/array-methods

Collapse
 
yogeswaran79 profile image
Yogeswaran

Hey there! I shared your article here t.me/theprogrammersclub and check out the group if you haven't already!

Collapse
 
pickleat profile image
Andy Pickle

Thanks Yogeswaran!