Filter() method of JavaScript

filter() method is a bit hard to understand for me at first. So I will share my thought to you.

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

by MDN web docs

So it would create a new array by running through all the old array with a function that returns a boolean. If true, put it into the new array.

Let’s say you have an array [3,2,2,4,8,8,8,6,9] called items. So when we want to get the values bigger than 8 or less than 8 and put them into a new array, we can use filter() method. As the screenshot, we can simply put our condition to return and there is it!

Leave a Comment

Your email address will not be published. Required fields are marked *

RSS
Follow by Email
LinkedIn
LinkedIn
Share