![]() |
VOOZH | about |
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.
_.pick(object, [paths]);In this Syntax:
Return Value: This method returns the new object.
_.pick() goes through the object and extracts the properties that match the given keys.Example 1: In this example, we use Lodash's _.pick() to create a new object with only password and username properties from the obj.
Output:
{password: "gfg@1234", username: "your_geeks"}
In this Example:
password and username properties from the original obj. _.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.
Output:
{x: 1, y: '2'}In this Example:
_.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);
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);
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);
});
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.