Lodash _.reject() method is the opposite of the _.filter() method and this method returns elements of the collection that predicate does not return true.
Syntax:
_.reject(collection, predicate);
Parameters:
- 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: In this example, we are printing those elements that do not match the passed parameter in the _.reject() method.
javascript
const _ = require( "lodash" );
let users = [
{ 'user' : 'Rohit' , 'age' : 25, 'active' : false },
{ 'user' : 'Mohit' , 'age' : 26, 'active' : true }];
let gfg = _.reject(users, function (o) { return !o.active; });
console.log(gfg);
|
Output:
[ { user: 'Mohit', age: 26, active: true } ]
Example 2: In this example, we are printing those elements that do not match the passed parameter(object) in the _.reject() method.
javascript
const _ = require( "lodash" );
let users = [
{
'employee' : 'Rohit' ,
'salary' : 50000,
'active' : false
},
{
'employee' : 'Mohit' ,
'salary' : 55000,
'active' : true
}
];
let gfg = _.reject(users,
{ 'salary' : 55000, 'active' : true });
console.log(gfg);
|
Output:
[ { employee: Rohit, salary: 50000, active: false } ]
Example 3: In this example, we are printing those elements that do not match the passed parameter(array) in the _.reject() method.
javascript
const _ = require( "lodash" );
let users = [
{
'employee' : 'Rohit' ,
'salary' : 50000,
'active' : false
},
{
'employee' : 'Mohit' ,
'salary' : 55000,
'active' : true
}
];
let gfg = _.reject(users, [ 'active' , false ]);
console.log(gfg);
|
Output:
[ { employee: Mohit, salary: 55000, active: true } ]
Example 4: In this example, we are printing those elements that do not match the passed parameter(string) in the _.reject() method.
javascript
const _ = require( "lodash" );
let users = [
{
'employee' : 'Rohit' ,
'salary' : 50000,
'active' : false
},
{
'employee' : 'Mohit' ,
'salary' : 55000,
'active' : true
}
];
let gfg = _.reject(users, 'active' );
console.log(gfg);
|
Output:
[ { employee: Rohit, salary: 50000, active: false } ]
Last Updated :
25 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...