Wednesday, September 25, 2024
Google search engine
HomeLanguagesJavascriptLodash _.pull() Method

Lodash _.pull() Method

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments