Lodash _.reject() Method - GeeksforGeeks

10 Sep, 2020
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, objects, numbers etc.The _.reject() method is opposite of _.filter() method and this method returns elements of the collection that predicate does not return true.
Syntax:
_.reject(collection, predicate)
Parameters: This method accepts two parameters as mentioned above and described below:
collection: This parameter holds the collection to iterate over.
iteratee: This parameter holds the function invoked per iteration.
Return Value: This method is used to return the new filtered array.
Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.
javascript
// Requiring the lodash library const _ = require("lodash");        // Original array var users = [  { 'user': 'Rohit', 'age': 25, 'active': false },  { 'user': 'Mohit', 'age': 26, 'active': true } ];   // Use of _.reject() method   let gfg = _.reject(users, function(o)         { return !o.active; });  // Printing the output console.log(gfg);
Output:
[ { user: 'Mohit', age: 26, active: true } ]

Example 2:
javascript
// Requiring the lodash library const _ = require("lodash");        // Original array var users = [  { 'employee': 'Rohit',     'salary': 50000,     'active': false   },  { 'employee': 'Mohit',     'salary': 55000,     'active': true   } ];   // Use of _.reject() method// The `_.matches` iteratee shorthandlet gfg = _.reject(users,     { 'salary': 55000, 'active': true });  // Printing the output console.log(gfg);
Output:
[ { employee: Rohit, salary: 50000, active: false } ]

Example 3:
javascript
// Requiring the lodash library const _ = require("lodash");        // Original array var users = [  { 'employee': 'Rohit',     'salary': 50000,     'active': false   },  { 'employee': 'Mohit',     'salary': 55000,     'active': true   } ];   // Use of _.reject() method// The `_.matchesProperty` iteratee shorthandlet gfg = _.reject(users, ['active', false]);  // Printing the output console.log(gfg);
Output:
[ { employee: Mohit, salary: 55000, active: true } ]

Example 4:
javascript
// Requiring the lodash library const _ = require("lodash");        // Original array var users = [  { 'employee': 'Rohit',     'salary': 50000,     'active': false   },  { 'employee': 'Mohit',     'salary': 55000,     'active': true   } ];   // Use of _.reject() method// The `_.property` iteratee shorthandlet gfg = _.reject(users, 'active');  // Printing the output console.log(gfg);
Output:
[ { employee: Rohit, salary: 50000, active: false } ]

Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.
JavaScript-Lodash
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
JavaScript | Promises
How to filter object array based on attributes?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript