how to use the pick function from lodash in javascript

The pick function from lodash is used to create an object composed of the picked object properties.

Syntax:

index.tsx
_.pick(object, [paths])
24 chars
2 lines

where:

  • object(Object): The source object.
  • [paths](...(string|string[])): The property paths to pick.

Example:

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

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

const pickedProperties = _.pick(sourceObject, ['name', 'age']);

console.log(pickedProperties);

// Output:
// { "name": "John", "age": 30 }
263 chars
16 lines

In the above example, pick function is used to create a new object with the name and age properties from the sourceObject. The resulting object is { "name": "John", "age": 30 }.

Note: In the example, require('lodash') is used to import the lodash library.

gistlibby LogSnag