Syntax, Parameter and Return Value
Syntax:
_.filter(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 filtered array.
Examples
Filtering Numbers:
JavaScript
var _ = require('lodash');
var numbers = [1, 2, 3, 4, 5, 6];
var evenNumbers = _.filter(numbers, n => n % 2 === 0);
console.log(evenNumbers);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[2, 4, 6]
This example demonstrates filtering an array to extract even numbers.
Objects with Specific Properties:
JavaScript
var users = [{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }];
var activeUsers = _.filter(users, {'active': true});
console.log(activeUsers);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'user': 'barney', 'active': true }, { 'user': 'pebbles', 'active': true }]
Here, _.filter() is used to retrieve objects that have a specific property value.
Using a Complex Predicate Function:
JavaScript
var products = [{ 'name': 'apple', 'type': 'fruit', 'quantity': 5 },
{ 'name': 'carrot', 'type': 'vegetable', 'quantity': 0 },
{ 'name': 'banana', 'type': 'fruit', 'quantity': 3 }];
var availableFruits = _.filter(products, p => p.type === 'fruit' && p.quantity > 0);
console.log(availableFruits);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'name': 'apple', 'type': 'fruit', 'quantity': 5 }, { 'name': 'banana', 'type': 'fruit', 'quantity': 3 }]
An example showing filtering based on more than one condition.
Filtering Based on a List of Values:
JavaScript
var values = [1, 2, 3, 4, 5];
var toFilter = [2, 4];
var filteredValues = _.filter(values, v => !toFilter.includes(v));
console.log(filteredValues);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 3, 5]
Demonstrates filtering out values based on a list of undesired elements.
Frequently Asked Questions
Can _.filter() be used on non-array collections?
Yes, it works on objects too, creating an array of values that pass the predicate test based on the object's properties.
How does _.filter() handle undefined or null values?
Undefined or null values are treated like any other value; if they meet the predicate condition, they will be included in the result.
Is _.filter() efficient for large datasets?
While efficient for most datasets, performance depends on the complexity of the predicate function and dataset size; testing for specific cases is recommended.
Conclusion
Lodash's _.filter() method is a robust and versatile tool for extracting subsets of data from collections based on specified criteria. It streamlines data processing, enabling more precise and efficient data handling in JavaScript. The method's flexibility and ease of use make it an essential part of a JavaScript developer's toolkit.
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.