DEV Community

Isabella
Isabella

Posted on

Javascript: .forEach and array.filter explained

//Create and array of tv shows. Loop through and print each show to the console

let shows =['bachelor', 'big brother','weel of fourtune']
shows.forEach(show => console.log(show))

We`re going to run once for each element in the array,we have 3 elements in the array. Each time is going to run is,going to pick each element from the array,and then plug it into 'show'. They are going to be console log(show).

//Create and array of numbers

let num=[1,2,3,4,5,6]

//Return a new array of numbers that includes every even number from the previous Arrays

let onlyEvens = arr => arr.filter( n => n%2 === 0 )

We want to get just the even numbers ,so we will use a filter method with modulus . The cool thing about filter is that it runs once for each element in the array and it checks to see if something is true. If it is true it includes that in the new array.

n => n%2 === 0 ) so were going to take in an "n", and well say n is % (modulus) 2 ,and that equals 0.

arr => arr.filter(n => n%2 === 0 ) This is an arrow function that will work with any array. We named our parameter 'arr'.

Then I runned the filter method (arr.filter(n => n%2 === 0 ) ) on that array. Is same as running the "map", it`s filter another method build into JS. What the 'filter' will do it is that is going to help us set up a new array, just as like "map" set up a new array, filter method set up also a new array.

To add a value to this new array, this statement (n => n%2 === 0 ) winds up to be true. If is not true, we don`t add that value into the array. So the very first time this runs, we are filter throught 'nums' because 'nums' is the array that we pass in. We called only evens and we pass in nums console.log(onlyEvens(nums)) So wherever we see 'arr','arr' is 'nums' just how we declare the function values in console.log.

"filter" is going to run 6 times because their are 6 numbers,6 elements in the array let num=[1,2,3,4,5,6]

The very first time nums will run, will be 1. Does 1%2=0? NO 1 is not an even number so thats a false.S o we dont add 1 to our new array. Next we try with all the numbers from the array. So even numbers will be 2,4,6 and will be added to our new array.

This new array arr.filter(n => n%2 === 0 ) is created right here, and something specific is going to happen.It is an implicit return. That means the new array we just created it gets returned to where it is called ( console.log(onlyEvens(nums)) ) to (nums). So [2,4,6] is going to be putted into the new array.

The 'filter' is going to create a new array for us => arr.filter(n => n%2 === 0 ) , and whenever is done creating that new array ,it gets returned to where it was called. To nums here in the console.log(onlyEvens(nums)) .

let shows =['bachelor', 'big borther','weel of fourtune']
shows.forEach(show => console.log(show))
let num=[1,2,3,4,5,6]
let onlyEvens = arr => arr.filter( n => n%2 === 0 )
console.log(onlyEvens(nums))

Top comments (0)