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.
- iterate: 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
// Requiring the lodash library
const _ = require("lodash");
// Original array
let 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: In this example, we are printing those elements that do not match the passed parameter(object) in the _.reject() method.
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let users = [
{
'employee': 'Rohit',
'salary': 50000,
'active': false
},
{
'employee': 'Mohit',
'salary': 55000,
'active': true
}
];
// Use of _.reject() method
// The `_.matches` iteratee shorthand
let gfg = _.reject(users,
{ 'salary': 55000, 'active': true });
// Printing the output
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
// Requiring the lodash library
const _ = require("lodash");
// Original array
let users = [
{
'employee': 'Rohit',
'salary': 50000,
'active': false
},
{
'employee': 'Mohit',
'salary': 55000,
'active': true
}
];
// Use of _.reject() method
// The `_.matchesProperty` iteratee shorthand
let gfg = _.reject(users, ['active', false]);
// Printing the output
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
// Requiring the lodash library
const _ = require("lodash");
// Original array
let users = [
{
'employee': 'Rohit',
'salary': 50000,
'active': false
},
{
'employee': 'Mohit',
'salary': 55000,
'active': true
}
];
// Use of _.reject() method
// The `_.property` iteratee shorthand
let gfg = _.reject(users, 'active');
// Printing the output
console.log(gfg);
Output:
[ { employee: Rohit, salary: 50000, active: false } ]
Lodash _.reject() Method - FAQs
Can _.reject() work with arrays of objects in Lodash?
Yes, _.reject() can be used with arrays of objects to exclude objects that match certain conditions based on their properties.
What happens if all elements are rejected by _.reject()?
If all elements match the predicate, _.reject() returns an empty array, as no elements remain that do not satisfy the predicate.
How does _.reject() handle empty collections?
If the collection is empty, _.reject() returns an empty array since there are no elements to process.
Does _.reject() maintain the order of elements?
Yes, _.reject() maintains the order of elements from the original collection in the returned array.
Can _.reject() be used with asynchronous functions?
_.reject() is synchronous and does not handle asynchronous functions directly. Use Promises or async handling techniques for asynchronous filtering.
Similar Reads
Lodash _.countBy() Method Lodash _.countBy method creates an object composed of keys generated from the results of running each element of collection through iteratee. The corresponding value of each key is the number of times the key was returned by iterate. Syntax:_.countBy(collection, [iteratee=_.identity])Parameters: Thi
3 min read
Lodash _.every() Method Lodash _.every() method checks if the predicate returns true for all elements of the collection and iteration is stopped once the predicate returns falsely. Note: Also this method returns true for empty collections because everything is true for elements of empty collections. Syntax_.every(collectio
3 min read
Lodash _.filter() Method The Lodash _.filter() method iterates over a collection (array or object) and returns a new array of elements that meet a specified condition (predicate), enabling efficient data filtering and extraction.Note: This method is not similar to the _.remove() method as this method returns a new array. Sy
3 min read
Lodash _.find() Method The Lodash _.find() method searches through a collection (array or object) and returns the first element that satisfies a specified condition (predicate). Itâs useful for quickly locating a matching item within a collection. If no match is found, it returns undefined.Syntax_.find(collection, predica
2 min read
Lodash _.findLast() Method Lodash _.findLast() method iterates over the elements in a collection from the right to the left. It is almost the same as _.find() method. Syntax:_.findLast(collection, predicate, fromIndex);Parameters:collection: It is the collection that the method iterates over.predicate: It is the function that
3 min read
Lodash _.flatMap() Method Lodash _.flatMap() method creates a flattened array of values by running each element in the collection through iterate and flattening the mapped results. Syntax:_.flatMap(collection, iteratee);Parameters: collection: This parameter holds the collection to iterate over.iterate: This parameter holds
3 min read
Lodash _.flatMapDeep() Method Lodash _.flatMapDeep() method creates a flattened array of values by running each element in the given collection through the iteratee function and recursively flattens the mapped results. It is similar to the _.flatMap() method. Syntax:_.flatMapDeep(collection, iteratee);Parameters:collection: It i
2 min read
Lodash _.flatMapDepth() Method Lodash's _.flatMapDepth() method flattens an array by applying a function to each element and recursively flattening the results up to a specified depth. It's similar to _.flatMap() but allows control over the flattening depth.Syntax:_.flatMapDepth( collection, iteratee, depth )Parameters: This meth
2 min read
Lodash _.forEach() Method Lodash _.forEach() method iterates over elements of the collection and invokes iterate for each element.Syntax:_.forEach(collection, [iteratee = _.identity]);Parameters:collection: This parameter holds the collection to iterate over.iteratee: It is the function that is invoked per iteration.Return V
2 min read
Lodash _.forEachRight() Method Lodash _.forEachRight() method iterates over elements of collection from right to left. It is almost the same as the _.forEach() method.Syntax:_.forEachRight( collection, iteratee );Parameters: collection: This parameter holds the collection to iterate over.iteratee: It is the function that is invok
2 min read