Lodash _.pickBy() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.pickBy() method is used to return a copy of the object that composed of the object properties predicate returns truthy for.
Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!
Syntax:
_.pickBy( object, predicate )
Parameters: This method accepts two parameters as mentioned above and described below:
- object: This parameter holds the source object.
- predicate: This parameter holds the function that is invoked for every property. It is an optional value.
Return Value: This method returns the new object.
Example 1:
Javascript
// Requiring the lodash library const _ = require("lodash"); // The source objectvar obj = { Name: "GeeksforGeeks", password: 123456, username: "your_geeks"} // Using the _.pickBy() method console.log(_.pickBy(obj, _.isLength)); |
Output:
{'password': 123456}Example 2:
Javascript
// Requiring the lodash library const _ = require("lodash"); // The source objectvar obj = { 'x': 1, 'y': '2', 'z': 3 }; // Using the _.pickBy() method console.log(_.pickBy(obj, _.isNumber)); |
Output:
{'x': 1, 'z': 3}
