Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value:
4.
Examples 
4.1.
Rejecting Based on a Simple Condition:
4.2.
JavaScript
4.3.
Filtering Out Objects with a Specific Property:
4.4.
JavaScript
4.5.
Using a Complex Predicate Function:
4.6.
JavaScript
4.7.
Rejecting Strings in an Array:
4.8.
JavaScript
5.
Frequently Asked Questions 
5.1.
How does _.reject() differ from _.filter()?
5.2.
Can _.reject() be used with non-array collections?
5.3.
Is _.reject() efficient for large datasets?
6.
Conclusion
Last Updated: Mar 27, 2024

Lodash _.reject() Method

Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

Filtering out elements from a collection that do not meet certain criteria is a common operation in data processing. Lodash simplifies this with the _.reject() method, which is essentially the opposite of _.filter(). It creates an array of elements for which a provided function returns false. 

Lodash _.reject() Method

This article will discuss the _.reject() method, explaining its syntax, use cases, and advantages, supported by examples and FAQs.

Why This Function is Used

The _.reject() function is used to remove elements from a collection based on a specified condition. It is particularly useful in scenarios where certain elements need to be excluded, such as filtering out invalid data, removing unwanted items, or applying negative criteria. This method offers a clear and concise way to express such exclusion logic, enhancing code readability and maintainability.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

JavaScript

var _ = require('lodash');

var numbers = [1, 2, 3, 4, 5, 6];

var rejectedNumbers = _.reject(numbers, n => n % 2 === 0);

console.log(rejectedNumbers);

 Output: 

[1, 3, 5]


Demonstrates rejecting even numbers from an array.

Filtering Out Objects with a Specific Property:

  • JavaScript

JavaScript

var users = [{ 'user': 'barney', 'active': false },

            { 'user': 'fred', 'active': true },

            { 'user': 'pebbles', 'active': false }];

var inactiveUsers = _.reject(users, 'active');

console.log(inactiveUsers);

 Output: 

[{ 'user': 'barney', 'active': false }, { 'user': 'pebbles', 'active': false }]


Shows rejecting objects where the active property is true.

Using a Complex Predicate Function:

  • JavaScript

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);

Output: 

[{ 'name': 'cucumber', 'weight': 1 }]


An example of rejecting items based on their weight.

Rejecting Strings in an Array:

  • JavaScript

JavaScript

var strings = ['hello', 'world', 'lodash', 'javascript'];

var shortStrings = _.reject(strings, s => s.length > 5);

console.log(shortStrings);

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
Lodash _.negate() Method
Next article
Lodash _.gte() Method
Live masterclass