In Lodash, we use the _.filter()
method to find items in an array based on specific criteria. This method iterates over all the array elements and returns those elements for which the predicates return a truthy value.
_.filter(collection, predicate)
This method accepts the following parameters:
collection
: This is the collection to be iterated.predicate
: This is the function that is checked against the values of the array in every iteration.This method returns a new array containing the filtered elements.
Let’s look at an example of finding items in an array based on certain criteria using the _.filter()
method in the code snippet below:
In the HTML tab:
lodash
script.In the JavaScript tab:
_.filter()
method to find all the people whose designation is Software Engineer
.