Filter

Array -> Array of items that match a condition

You've hacked into the school's computer system, and just in time. The grades are in, but you're not too proud of your performance. That's okay, you have a plan: you're going to create a fake report card.

It would be great if you could filter the scores that your parents will see.

filter takes a matching condition function and only returns items that result in true. As an example, look at isA below:

functionisA(x) {
return x ==='a';
}

Like all of the methods in this chapter, filter is already part of the Array.prototype, so you can run it following any array. Each item in the array is passed into the params of the condition function, one by one. Learn more.

In this example, map transformed an object with keys of 'title', 'instructor', 'name', 'score' and 'grade', to an array of just scores. Values weren't changed, but rather limited to a smaller subset of scores.

map is powerful. Let's see what you can do with it.

Those D & F grades would look a lot better if they suddenly became A's.

Let's go back to before we filtered out the bad grades, and instead change the grades to A's.

forEach

Array -> run a function for each item

You've updated your grades, but they're still in an array. It's time to loop over them and log them to the console.

To open the console, go to View > Developer > Toggle Developer Tools. Or press cmd+opt+I on Mac, ctrl+alt+I on Windows.

forEach has a lot in common with map, but there is a big difference. Understanding that difference is important for grasping the difference between:

functional & imperative programming

pure & impure functions

Know it or not, you're probably already used to "imperative" programming.