How to use Lodash _.reject() Method In Lodash
In this approach, we are using the _.reject method from Lodash with a predicate that checks for equality with ‘banana’, creating a new array excluding ‘banana’ and keeping other elements in the list.
Command to install lodash:
npm i lodash
Example: The below example uses the reject function to remove an element from a list in lodash.
const _ = require('lodash');
let list = ['apple', 'banana', 'cherry', 'orange'];
let res = _.reject(list, item => item === 'banana');
console.log(res);
Output:
[ 'apple', 'cherry', 'orange' ]
How to Remove an Element from a list in Lodash?
To remove an element from a list, we employ Lodash functions such as without, pull, or reject. These methods enable us to filter out elements from the list based on specific conditions, effectively excluding those that match the criteria.
Below are the approaches to remove an element from a list in lodash:
Table of Content
- Using Lodash _.without() Method
- Using Lodash _.pull() Method
- Using Lodash _.reject() Method
Contact Us