Lodash _.pick() Method

The _.pick() method in Lodash is incredibly handy. It allows you to create a new object containing only the specified properties from the original object. Essentially, it’s like plucking out a subset of properties from the original object and creating a fresh one with just those properties. This makes it super convenient when you only need certain bits of data from a larger object.

Syntax:

_.pick(object, [paths]);

Parameters:

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

Return Value:

This method returns the new object.

Example 1: In this example,

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

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

Output:

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

Example 2:  

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'}

Contact Us