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 Keys in Lodash?
Sometimes while working with objects in JavaScript we want to filter objects based on keys. We can easily do this by using Lodash, a popular JavaScript library that provides several inbuilt functions to achieve this easily. In this article, we are going to discuss how we can Filter Objects by Keys using 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 Keys in Lodash
Using Lodash's pick() Method
The pick() method in Lodash allows us to select specific keys from an object. It creates a new object containing only the specified keys. This method allows us to extract specific key-value pairs by directly specifying the keys we want to keep.
Syntax
_.pick(object, [paths])
Parameters
- object: The source object to pick properties from
- paths: Array of property names to pick
Example
const _ = require("lodash");
// Original object
const obj = {
name: "Ayush",
age: 21,
city: "Kanpur",
profession: "Tech Writer"
};
// Filter object by keys
const filteredObj = _.pick(obj, ["name", "profession"]);
console.log("Original object:", obj);
console.log("Filtered object:", filteredObj);
Original object: { name: 'Ayush', age: 21, city: 'Kanpur', profession: 'Tech Writer' }
Filtered object: { name: 'Ayush', profession: 'Tech Writer' }
Using Lodash's omit() Method
The omit() method is just the reverse of the _.pick() method. In this method instead of selecting specific keys, it excludes the specified keys and keeps the rest. This method loops through the object's keys and checks whether each key is in the exclusion list. If a key is not in the exclusion list, it is added to the result object.
Syntax
_.omit(object, [paths])
Parameters
- object: The source object to omit properties from
- paths: Array of property names to omit
Example
const _ = require("lodash");
// Original object
const obj = {
name: "Ayush",
age: 21,
city: "Kanpur",
profession: "Tech Writer"
};
// Remove keys
const filteredObj = _.omit(obj, ["age", "city"]);
console.log("Original object:", obj);
console.log("Filtered object:", filteredObj);
Original object: { name: 'Ayush', age: 21, city: 'Kanpur', profession: 'Tech Writer' }
Filtered object: { name: 'Ayush', profession: 'Tech Writer' }
Comparison
| Method | Purpose | Use Case |
|---|---|---|
_.pick() |
Include specific keys | When you know which properties to keep |
_.omit() |
Exclude specific keys | When you know which properties to remove |
Practical Example
const _ = require("lodash");
const user = {
id: 123,
username: "john_doe",
password: "secret123",
email: "john@example.com",
role: "admin",
lastLogin: "2024-01-15"
};
// For public profile - pick only safe fields
const publicProfile = _.pick(user, ["id", "username", "email"]);
console.log("Public profile:", publicProfile);
// For database update - omit sensitive fields
const updateData = _.omit(user, ["password", "lastLogin"]);
console.log("Update data:", updateData);
Public profile: { id: 123, username: 'john_doe', email: 'john@example.com' }
Update data: { id: 123, username: 'john_doe', email: 'john@example.com', role: 'admin' }
Conclusion
Both _.pick() and _.omit() methods provide efficient ways to filter objects by keys in Lodash. Use pick() when you want to include specific properties, and omit() when you want to exclude certain properties from an object.
