What is the _.remove() method in Lodash?
Overview
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.
Syntax
_.remove(array, predicate)
The _.remove() method syntax
Parameters
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.
Return value
This method returns an array of all the removed elements.
Example
Let’s look at an example of the _.remove() method:
Explanation
In the HTML tab:
- Line 5: We import the
lodashscript.
In the JavaScript tab:
- Line 2: We create an array,
numbers, and populate it with a few values.
- Line 5: We use the
_.remove()method to remove all the elements that are less than 5. - Line 8: We print the original array,
numbers, on the console.
- Line 11: We print the array of the removed elements,
result, on the console.
Output
In the output, we see the original array mutated by the _.remove() method and another array that contains all the removed elements.