how to use the pick function from the underscore library in javascript

The _.pick function from the Underscore library in JavaScript is used to create a shallow copy of an object, containing only the specified properties. It takes an object and one or more property names as arguments and returns a new object with only those properties included.

Here's an example of how to use _.pick:

index.tsx
const _ = require('underscore');

const myObj = {
  name: 'John',
  age: 30,
  city: 'New York',
  country: 'USA'
};

const pickedObj = _.pick(myObj, 'name', 'age');

console.log(pickedObj);
// Output: { name: 'John', age: 30 }
228 chars
14 lines

In this example, we first import Underscore using the require statement. We then define an object called myObj with four properties.

We then use _.pick to create a new object called pickedObj, which contains only the name and age properties from myObj.

Finally, we log the pickedObj to the console, which outputs { name: 'John', age: 30 }.

gistlibby LogSnag