Open In App

Lodash _.reject() Method

Last Updated : 03 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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