Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Filter Object by Values in Lodash?
Sometimes we are given an object and we want to extract only the key-value pairs that meet certain criteria. In this article, we are going to discuss how we can filter objects by values in Lodash.
Prerequisite
- Lodash: Lodash is a popular JavaScript library used to deal with a variety of functions such as arrays, objects, strings, and more.
Install Lodash
We can install the Lodash library in the project by adding it via CDN or npm using the following command:
npm install lodash
Approaches to Filter Object by Values
- Using _.pickBy Method
- Using _.filter with Object.keys
- Using JavaScript Object.entries and Object.fromEntries
Using _.pickBy Method
In this approach, we use the _.pickBy method to filter objects by values. This method takes an object and a predicate function, iterates over each key-value pair, and includes only those entries for which the predicate returns true. It is the most straightforward approach when working with Lodash.
Syntax
_.pickBy(object, predicate)
Example
const _ = require('lodash');
const students = { ayush: 85, anshu: 92, smrita: 45, antima: 74 };
// Filter values greater than 80
const filteredStudents = _.pickBy(students, (value) => value > 80);
console.log(filteredStudents);
{ ayush: 85, anshu: 92 }
Using _.filter with Object.keys
This approach combines lodash _.filter with JavaScript's Object.keys to filter an object by its values. We first retrieve all the keys of the object, then filter those keys based on their associated values. Finally, we reconstruct the object using _.fromPairs.
Example
const _ = require('lodash');
const students = { ayush: 85, anshu: 92, smrita: 45, antima: 74 };
// Filter values greater than 80
const filteredKeys = Object.keys(students).filter((key) => students[key] > 80);
const filteredStudents = _.fromPairs(filteredKeys.map((key) => [key, students[key]]));
console.log(filteredStudents);
{ ayush: 85, anshu: 92 }
Using JavaScript Object.entries and Object.fromEntries
In this method, we use native JavaScript methods without requiring Lodash. Object.entries converts the object into an array of key-value pairs, which can be filtered using Array.filter. The resultant array is then converted back into an object using Object.fromEntries.
Example
const students = { kalpana: 85, neeraj: 92, aditi: 45, srishti: 74 };
// Filter values greater than 80
const filteredStudents = Object.fromEntries(
Object.entries(students).filter(([key, value]) => value > 80)
);
console.log(filteredStudents);
{ kalpana: 85, neeraj: 92 }
Comparison
| Method | Requires Lodash | Readability | Performance |
|---|---|---|---|
| _.pickBy | Yes | High | Good |
| _.filter + Object.keys | Yes | Medium | Good |
| Object.entries + Object.fromEntries | No | High | Best |
Conclusion
For filtering objects by values, _.pickBy is the cleanest Lodash approach, while Object.entries with Object.fromEntries provides a native JavaScript solution without external dependencies. Choose based on whether you're already using Lodash in your project.
