Semantic question regarding .map() and .filter()

When using .map() and .filter() methods together is there a preferred and/or recommended order to use them in?

While completing the ‘Use the filter Method to Extract Data from an Array’ problem in the ‘Functional Programming’ section of the JS curriculum you use both these methods together.

My solution (see below) passed, however, when looking at the solutions FCC provides I noticed that they all use .filter() before .map().
Is there a specific reason for this (i.e. preferred method, avoiding bugs in more complex code, better readability, etc.) or is it just how they chose to order them?

const filteredList = watchList
.map( movies => ({title: movies['Title'], rating: movies.imdbRating }))
.filter(score => parseFloat(score.rating) >= 8.0 );

FCC solution:

const filteredList = watchList
  .filter(movie => movie.imdbRating >= 8.0)
  .map(movie => ({ title: movie["Title"], rating: movie["imdbRating"] }));

In general, filter before map, because then the map has less work to do.

1 Like

Jeremy,

That makes perfect sense. Thank you for clarifying!

ZZ

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.