January 2020
Intermediate to advanced
470 pages
11h 13m
English
A common need that we will encounter is to filter the elements of an array according to a certain condition. The filter() method lets you inspect each element of an array in the same fashion as map(). The difference is that instead of producing a new element, the result of your function determines whether the input value will be kept in the output (if the function returned true) or if it will be skipped (if the function returned false). Also similar to map(), filter() doesn't alter the original array, but rather returns a new array with the chosen items.
See Figure 5.4 for a diagram showing the input and output:

Read now
Unlock full access