Syntax, Parameter and Return Value
Syntax:
_.reject(collection, [predicate=_.identity])
Parameters:
-
collection (Array|Object): The collection to iterate over.
- [predicate=_.identity] (Function): The function invoked per iteration.
Return Value:
(Array) - Returns the new array of filtered elements.
Examples
Rejecting Based on a Simple Condition:
JavaScript
var _ = require('lodash');
var numbers = [1, 2, 3, 4, 5, 6];
var rejectedNumbers = _.reject(numbers, n => n % 2 === 0);
console.log(rejectedNumbers);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 3, 5]
Demonstrates rejecting even numbers from an array.
Filtering Out Objects with a Specific Property:
JavaScript
var users = [{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': true },
{ 'user': 'pebbles', 'active': false }];
var inactiveUsers = _.reject(users, 'active');
console.log(inactiveUsers);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'user': 'barney', 'active': false }, { 'user': 'pebbles', 'active': false }]
Shows rejecting objects where the active property is true.
Using a Complex Predicate Function:
JavaScript
var items = [{ 'name': 'apple', 'weight': 0.5 },
{ 'name': 'banana', 'weight': 0.3 },
{ 'name': 'cucumber', 'weight': 1 }];
var heavyItems = _.reject(items, item => item.weight <= 0.5);
console.log(heavyItems);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'name': 'cucumber', 'weight': 1 }]
An example of rejecting items based on their weight.
Rejecting Strings in an Array:
JavaScript
var strings = ['hello', 'world', 'lodash', 'javascript'];
var shortStrings = _.reject(strings, s => s.length > 5);
console.log(shortStrings);

You can also try this code with Online Javascript Compiler
Run Code
Output:
['hello', 'world']
Demonstrates rejecting strings based on their length.
Frequently Asked Questions
How does _.reject() differ from _.filter()?
_.reject() returns an array of elements for which the predicate returns false, whereas _.filter() returns elements for which the predicate returns true.
Can _.reject() be used with non-array collections?
Yes, _.reject() can be used with objects, but it's most commonly used with arrays.
Is _.reject() efficient for large datasets?
_.reject() is generally efficient for large datasets, but performance can vary depending on the complexity of the predicate function.
Conclusion
Lodash's _.reject() method provides a straightforward and efficient way to filter out elements from a collection that do not meet a specific criterion. It's a valuable tool for data manipulation, allowing developers to easily exclude unwanted elements from their datasets.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.