Lodash _.pick() Method

Last Updated : 28 Apr, 2025

The Lodash _.pick() method creates a new object by selecting specified properties from an existing object. It returns an object containing only the requested keys and their values, making it useful for extracting and filtering specific data from larger objects.

Syntax

_.pick(object, [paths]);

In this Syntax:

  • object: This parameter holds the source object.
  • paths: This parameter holds the property paths to pick.

Return Value: This method returns the new object.

How does the Lodash _.pick() Method Work?

  • _.pick() goes through the object and extracts the properties that match the given keys.
  • The function returns a new object that only contains those selected properties.
  • If a property key doesn’t exist in the object, it is simply ignored.
  • The original object is not changed, a new object is created.

Example 1: In this example, we use Lodash's _.pick() to create a new object with only password and username properties from the obj.

JavaScript
// Requiring the lodash library  
const _ = require("lodash");

// The source object
let obj = {
    Name: "GeeksforGeeks",
    password: "gfg@1234",
    username: "your_geeks"
}
// Using the _.pick() method 
console.log(_.pick(obj, ['password', 'username']));

Output:

{password: "gfg@1234", username: "your_geeks"}

In this Example:

  • Here, we wanted to create a new object that only contains the password and username properties from the original obj.
  • So, we used _.pick() to select only those two properties, and the result is a new object with just those values.

Example 2:  In this example we use Lodash's _.pick() to create a new object containing only the x and y properties from obj.

JavaScript
// Requiring the lodash library  
const _ = require("lodash");

// The source object
let obj = { 'x': 1, 'y': '2', 'z': 3 };

// Using the _.pick() method 
console.log(_.pick(obj, ['x', 'y']));

Output:

{x: 1, y: '2'}

In this Example:

  • In this case, we picked the x and y properties from the obj and created a new object containing just those two properties.
  • Again, it’s a new object with the values we specified.

Use Cases of Lodash _.pick() Method

1. Data Filtering with _.pick()

The _.pick() method from Lodash is extremely useful for extracting only the necessary data from large or deeply nested objects. This is especially helpful when you want to remove sensitive or irrelevant fields before sending data to the frontend, storing it, or logging it. It helps streamline processing and reduce memory usage.

const fullUserData = {
userId: '123',
fullName: 'GeeksforGeeks',
emailAddress: 'gfg@example.com',
passwordHash: 'hashedpassword',
isAdmin: true,
// ...more properties
};
// Extract only the essential public fields
const publicUserData = _.pick(fullUserData, ['userId', 'fullName', 'emailAddress']);

console.log(publicUserData);

2. Object Transformation

When you need to reshape or clean up an object for a specific task, _.pick() helps by grabbing only the important parts. This makes your code easier to manage and avoids carrying around extra data you don’t need.

const ApiResponse = {
status: 'success',
data: {
id: '404',
name: 'GeeksforGeeks',
email: 'gfgexample.com',
// more data we don't need right now...
},
};
// We only need status and a few user details
const simplifiedResponse = {
status: ApiResponse.status,
user: _.pick(ApiResponse.data, ['id', 'name', 'email']),
};
console.log(simplifiedResponse);

3. API Responses

When building APIs, _.pick() is great for sending only the useful parts of your data to the client. This keeps responses clean, saves bandwidth, and keeps things secure by hiding sensitive informarion.

app.get('/user/:id', (req, res) => {
const userId = req.params.id;

// Let's say we fetched user data from the database
const user = {
id: '420',
name: 'GeeksforGeeks',
email: 'gfg@example.com',
password: 'hashedpassword', // we don't want to send this!
isAdmin: true,
};

// Send only safe and needed fields
const safeUserData = _.pick(user, ['id', 'name', 'email']);
res.json(safeUserData);
});

Conclusion

The _.pick() method in Lodash is a powerful and efficient way to extract specific properties from an object, making your code cleaner and more manageable. It simplifies working with large or complex objects by allowing you to focus on the data that matters. However, be mindful of using it for deeply nested properties to avoid unnecessary complexity in your code.

Comment