Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Use JavaScript ES2019 flatMap to Map and Filter an Array

    Mike SherovMike Sherov
    javascriptJavaScript

    ES2019 introduces the Array.prototype.flatMap method. In this lesson, we'll investigate a common use case for mapping and filtering an array in a single iteration. We'll then see how to do this using Array.prototype.reduce, and then refactor the code to do the same thing using the ES2019 .flatMap method.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: Once again, we're looking at our next JS application. This time, we're looking at a filtered average or students per session. I can click on A or B and get different filtered average, which is the average number of students that attended a session by that site. If we again look at our JSON, we see that each site has an ID. We can look at the averages for A or B by filtering them.

    We look at our sites.vue file. We see that the filtered average is the filtered average count property, which is again a computed property based off of our records. Whereas before we were flat mapping or we just pulled AM and PM off of every record, in this case we wanted to do something different.

    Prior to ES2019, once again, we didn't have flatMap function. We would use a foreach and push elements onto an array that we would aggregate ourselves. In this case, we compare the ID of the record and see if it's equivalent to the ID property that we set in Nuxt.js.

    Again, when you click on each of the IDs, you see that we're setting the ID property equal to the site ID that we're clicking on. This ID is the ID that we're filtering. Record.ID is the ID of the record. Only in the case where the IDs match do we push the records AM and PM value onto our overall list of numbers to be averaged.

    We can convert this to flatMap too. Again, we could say const ounce = this.records.flatMap. Here's the cool part about flatMap. If you pass back an empty array, it'll push nothing onto the stack of averages to be counted. If I save this now, we'd see that it's going to do a zero over zero, which is NaN.

    In this case we wanted to do our condition from before. We want to say, "If the IDs match, then we could return record AM and PM. If IDs don't match, just return an empty array." If we run this again, we'll see that we now get the functionality that we had before.

    Let's look at this again. The first time, flatMap is the first function that actually in JavaScript allows us to both map and filter at the same time. If you're using a filter function, if you were doing records.filter in JavaScript, you only get a chance to either include or exclude a record.

    You would have to do something like return this record ID equals that record ID. This will just either include or exclude the record. If you do a map, map allows you to turn those objects into arrays, but there's nothing in ES pre-2019 that allows you to both filter and map.

    That's where I think flatMap is pretty cool. It allows you to pass back an array of records, or it allows you to pass back an empty array and say, "You know what? Don't give me any values from this record." Again, if we save this and run it, we've achieved the same functionality as before.