Lodash _.filter() Method - GeeksforGeeks

06 Sep, 2020
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, objects, numbers etc.
The _.filter() method iterates over elements of collection, returning an array of all elements predicate returns true.
Note: This method is not similar to the _.remove() method as this method returns a new array.
Syntax:
_.filter( collection, predicate )
Parameters: This method accepts two parameters as mentioned above and described below:
collection: This parameter holds the collection to iterate over.
predicate: This parameter holds the function invoked per iteration.
Return Value: This method returns the new filtered array.
Example 1:
// Requiring the lodash library const _ = require("lodash");       // Original array var users = [  { 'user': 'luv',    'salary': 36000,    'active': true },  { 'user': 'kush',     'salary': 40000,    'active': false }];  // Using the _.filter() methodlet 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:
// Requiring the lodash library const _ = require("lodash");       // Original array var users = [  { 'user': 'luv',    'salary': 36000,    'active': true },  { 'user': 'kush',     'salary': 40000,    'active': false }];  // Using the _.filter() method// The `_.matches` iteratee shorthandlet filtered_array = _.filter(users,     { 'salary': 36000, 'active': true });  // Printing the output console.log(filtered_array);
Output:
[ { user: 'luv', salary: 36000, active: true } ]
Example 3:
// Requiring the lodash library const _ = require("lodash");       // Original array var users = [  { 'user': 'luv',    'salary': 36000,    'active': true },  { 'user': 'kush',     'salary': 40000,    'active': false }];  // Using the _.filter() method// The `_.matchesProperty` iteratee shorthandlet filtered_array =  _.filter(users, ['active', false]);  // Printing the output console.log(filtered_array);
Output:
[ { user: 'kush', salary: 40000, active: false } ]
Example 4:
// Requiring the lodash library const _ = require("lodash");       // Original array var users = [  { 'user': 'luv',    'salary': 36000,    'active': true },  { 'user': 'kush',     'salary': 40000,    'active': false }];  // Using the _.filter() method// The `_.property` iteratee shorthandlet filtered_array =  _.filter(users, 'active');  // Printing the output console.log(filtered_array);
Output:
[ { user: 'luv', salary: 36000, active: true } ]
JavaScript-Lodash
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Convert a string to an integer in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?
File uploading in React.js
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?