A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/lodash-_-filter-method/ below:

Lodash _.filter() Method - GeeksforGeeks

Lodash _.filter() Method

Last Updated : 03 Sep, 2024

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.

Syntax:
_.filter( collection, predicate )
Parameters: Return Value:

This method returns the new filtered array.

Example 1: In this example, we are printing the user whose active is false by the use of the function. so we are passing the function in the _.filter() method.

javascript
// Requiring the lodash library 
const _ = require("lodash");

// Original array 
let users = [
    {
        'user': 'luv',
        'salary': 36000,
        'active': true
    },
    {
        'user': 'kush',
        'salary': 40000,
        'active': false
    }
];

// Using the _.filter() method
let filtered_array = _.filter(
    users, function (o) {
        return !o.active;
    }
);

// Printing the output 
console.log(filtered_array);

Output:

[ { user: 'kush', salary: 40000, active: false } ]

Example 2: In this example, we are printing the user whose active is true by the use of the object. so we are passing the object in the _.filter() method.

javascript
// Requiring the lodash library 
const _ = require("lodash");

// Original array 
let users = [
    {
        'user': 'luv',
        'salary': 36000,
        'active': true
    },
    {
        'user': 'kush',
        'salary': 40000,
        'active': false
    }
];

// Using the _.filter() method
// The `_.matches` iteratee shorthand
let filtered_array = _.filter(users,
    { 'salary': 36000, 'active': true }
);

// Printing the output 
console.log(filtered_array);

Output:

[ { user: 'luv', salary: 36000, active: true } ]

Example 3: In this example, we are printing the user whose active is false by the use of the array. so we are passing the array in the _.filter() method.

javascript
// Requiring the lodash library 
const _ = require("lodash");

// Original array 
let users = [
    {
        'user': 'luv',
        'salary': 36000,
        'active': true
    },
    {
        'user': 'kush',
        'salary': 40000,
        'active': false
    }
];

// Using the _.filter() method
// The `_.matchesProperty` iteratee shorthand
let filtered_array =
    _.filter(users, ['active', false]);

// Printing the output 
console.log(filtered_array);

Output:

[ { user: 'kush', salary: 40000, active: false } ]

Example 4: In this example, we are printing the user who has active property. so we are passing the active property in the _.filter() method.

javascript
// Requiring the lodash library 
const _ = require("lodash");

// Original array 
let users = [
    {
        'user': 'luv',
        'salary': 36000,
        'active': true
    },
    {
        'user': 'kush',
        'salary': 40000,
        'active': false
    }
];

// Using the _.filter() method
// The `_.property` iteratee shorthand
let filtered_array =
    _.filter(users, 'active');

// Printing the output 
console.log(filtered_array);

Output:

[ { user: 'luv', salary: 36000, active: true } ]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4