Suppose you had a list of students in a classroom, and you want to know how many are girls. You might write something like this:
filter function attached to every array!
... read the whole story at dev.to.
Filter returns a new array that is a subset of the array you call it on, wherein the callback function returns either true or false. A
true value will include that item in the new array. A
false value will leave it out. This makes it easy to write filters that can accommodate however simple or complex you need. In our class, lets say we instead want to find all the students that are eleven, and have the letter 'e' in their name. With a filter, we just need to return