
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 used when working with large data structures.
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);
Output
{ 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, we filter those keys based on their associated values. Then, we 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);
Output
{ ayush: 85, anshu: 92 }
Using JavaScript (Object.entries and Object.fromEntries)
In this method, we use native JavaScript methods such as Object.entries and Object.fromEntries. This method provides a clean way to filter an object by its values. Object.entries method converts the object into an array of key-value pairs, which can be filtered using the Array.filter method. 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);
Output
{ kalpana: 85, neeraj: 92 }