The _.pull() method is used to remove all the given values from a given array.
Syntax:
_.pull(array, [values])
Parameters: This method accepts two or more parameters as mentioned above and described below:
- Array: This parameter holds the query array.
- values: This parameter holds one or multiple element that need to be removed from array.
Return Value: It return an array that contains remaining element.
Example 1: This example removes all the values from array that matches with given values.
const _ = require( 'lodash' ); let ar = [1, 2, 3, 4, 5] let value = _.pull(ar, 3, 5) console.log(value) |
Here, const _ = require('lodash')
is used to import the lodash library into the file.
Output:
[ 1, 2, 4 ]
Example 2: This example removes all the values from array that matches with given values.
const _ = require( 'lodash' ); let ar = [1, 2, 3, 1, 3, 4, 1, 5] let value = _.pull(ar, 1, 5) console.log(value) |
Output:
[ 2, 3, 3, 4 ]
Example 3: This example removes all the values from array that matches with given values.
const _ = require( 'lodash' ); let ar = [ 'a' , 'b' , 'c' , 'd' ] let value = _.pull(ar, 'c' ) console.log(value) |
Output:
[ 'a', 'b', 'd' ]
Note: It will not work in normal JavaScript because it requires the library lodash to be installed.
Reference: https://lodash.com/docs/4.17.15#pull