How to use pick function In Lodash
In this approach, we are using the pick function from Lodash with an object and an array of keys to include, creating a new object res that contains only the specified keys ‘a‘ and ‘c’ from the original object obj.
Syntax:
_.pick(object, [props])
Example: The below example uses the pick function to filter the keys of an object with lodash.
const _ = require('lodash');
let obj = { a: 1, b: 2, c: 3 };
let res = _.pick(obj, ['a', 'c']);
console.log(res);
Output:
{ a: 1, c: 3 }
How to Filter Keys of an Object with Lodash ?
Filtering Keys of an Object is used for selectively including or excluding keys based on criteria, facilitating data manipulation and customization.
Below are the approaches to filter keys of an object with Lodash:
Table of Content
- Using pick function
- Using omit function
- Using pickBy function
Installing the Lodash library using the below command:
npm i lodash
Contact Us