Filter

Filter is a higher-order function that allows us to distinguish and keep only certain elements of a collection based on a Boolean predicate. Of course, filtering is particularly useful when removing certain elements from a collection based on a certain condition. Take the following code as an example:

function myJS(){    let array = [1, 2, 3];    let arrayEvenNumbers = array.filter(current => current % 2 == 0);    // arrayEvenNumbers == [2]}

Filtering is a great way to avoid loops and nested conditions in order to extract some desired dataset.

Get Mastering The Faster Web with PHP, MySQL, and JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.