Lodash | _.remove() Method
The _.remove() method is used to remove all elements from the array that predicate returns True and returns the removed elements.
Syntax:
_.remove(array, function)
Parameters: This method accept two parameters as mentioned above and described below:
- array: This parameter holds the array that need to be modify.
- function: This parameter holds the function that invoked per iteration.
Return Value: It returns an array of removed elements.
Example: Here all the elements returning true which are even elements. The function is invoked on all the elements (n) of the array.
let x = [1, 2, 3, 4, 5]; let even = _.remove(x, function(n) { return n % 2 == 0; }); console.log('Origianal Array ', x); console.log('Removed element array ', even); |
Here, const _ = require('lodash') is used to import the lodash library into the file.
Output:
Origianal Array [ 1, 3, 5 ] Removed element array [ 2, 4 ]
Example 2: This example removes all the vowels from an array containing alphabets and store them in a new array.
const _ = require('lodash'); let x = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']; let vowelArray = _.remove(x, function(n) { let vowels = ['a', 'e', 'i', 'o', 'u']; for (let i = 0; i < 5; i++) { if (n === vowels[i]) { return true; } } }); console.log('Origianal Array ', x); console.log('Removed element array ', vowelArray); |
Output:
Origianal Array [ 'b', 'c', 'd', 'f', 'g', 'h' ] Removed element array [ 'a', 'e', 'i' ]
Example 3: This example removes all the integers from a given array containing floats, characters, and integers.
let x = ['a', 'b', 1, 5.6, 'e', -7, 'g', 4, 10.8]; let intsArray = _.remove(x, function(n) { return Number.isInteger(n); }); console.log('Origianal Array ', x); console.log('Removed element array ', intsArray); |
Output:
Origianal Array [ 'a', 'b', 5.6, 'e', 'g', 10.8 ] Removed element array [ 1, -7, 4 ]
Note: This will not work in normal JavaScript because it requires the library lodash to be installed.
Reference: https://lodash.com/docs/4.17.15#remove
Recommended Posts:
- Lodash | _.nth() Method
- Lodash | _.take() Method
- Lodash | _.xor() Method
- Lodash | _.find() Method
- Lodash | _.zipObject() Method
- Lodash | _.differenceWith() Method
- Lodash | _.zipWith() Method
- Lodash | _.cloneDeep() Method
- Lodash | _.sampleSize() Method
- Lodash | _.clone() Method
- Lodash | _.pullAll() Method
- Lodash | _.head() Method
- Lodash | _.takeRight() Method
- Lodash | _.fromPairs() Method
- Lodash | _.pullAt() Method
- Lodash | _.pull() Method
- Lodash | _.castArray() Method
- Lodash | _.sortedLastIndex() Method
- Lodash | _.flattenDeep() and _.flattenDepth() Method
- Lodash | _.tail() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



