forEach, map, and filter

Introduction
In this subunit, we'll take a look at three of the most common array methods: forEach, map, and filter. We'll explore the functionality of these methods and when to use them. We touched on higher order functions when we taught you about JavaScript functions back in the Web Fundamentals unit, in this subunit we'll dive deeper into what they are and how they can be used to write more elegant code. You'll do some practice with each of these methods as we go along because they are essential for working with modern JavaScript frameworks and codebases.
forEach
Goals
Understand what foreach does
Write our own version of the forEach function
Why do I need to know this
Write cleaner and more declarative code
Use built in higher order functions and callback functions
Foundation for modern libraries and frameworks
What does it do
Loops through an array
Runs a callback function for each value in the array and then returns undefined
forEach will always return undefined - no matter what
An Example
Let's Build Our Own
Loops through an array
Runs a callback function on each value in the array
returns undefined
An Example
Remember - forEach always returns undefined!
When You Would Use forEach
You want to iterate over an array, but the return value of your callback is not important
Almost all of the time there are better options...
map
Goals
Understand what map does
Write your own version of map
What does it do
Creates a new array
Iterates through an array
Runs a callback function for each value in the array
Adds the result of that callback function to the new array
Returns the new array
map always returns a new array of the same length
An Example
How Does It Work
Creates a new array
Loops through an array
Runs a callback function for each value in the array
Pushes result of the callback function to the new array
Returns the new array
Using map In A Function
When You Would Use Map
You want to "transform” an array into another array of the same length
You do not want to overwrite an existing array and instead return a new copy
filter
Goals
Understand what filter does
Write your own filter function
What does it do
Creates a new array
Loops through an array
Runs a callback function on each value in the array
If the callback function returns true, that value is pushed to the new array
If the callback function returns false, that value will not be included in the new array
the result of the callback will always be evaluated into a boolean
An Example
Another Example
How Does It Work
Creates a new array
Iterates through an array
Runs a callback function on each value in the array
If the callback function returns true, that value will be added to the new array
If the callback function returns false, that value will be ignored from the new array
Using Filter In A Function
When You Would Use Filter
You want to "transform" an array into another array of the same length or smaller length depending on a condition
You want to see how many elements in an array satisfy a certain condition
You do not want to modify the existing array you are filtering