The _.remove()
method in Lodash removes all the elements from an array that returns a truthy value for the specified predicate.
This method will mutate the original array and return an array of all the removed elements.
_.remove(array, predicate)
This method accepts the following parameters:
array
: This is the array to be inspected.predicate
: This is the function that is checked against the array's values in every iteration.This method returns an array of all the removed elements.
Let’s look at an example of the _.remove()
method:
In the HTML tab:
lodash
script.In the JavaScript tab:
numbers
, and populate it with a few values._.remove()
method to remove all the elements that are less than 5.numbers
, on the console.result
, on the console.In the output, we see the original array mutated by the _.remove()
method and another array that contains all the removed elements.