Friday, December 27, 2024
Google search engine
HomeLanguagesJavascriptLodash _.pickBy() Method

Lodash _.pickBy() Method

Lodash _.pickBy() method is used to return a copy of the object that is composed of the object properties predicate returns truthy for.

Syntax:

_.pickBy( object, [predicate] )

Parameters:

  • object(Object) parameter holds the source object.
  • predicate(function) 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: In this example, we are using the _pickBy() method in which we are passing the _.isLength() method.

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
// The source object
let obj = {
    Name: "neveropen",
    password: 123456,
    username: "your_neveropen"
}
 
// Using the _.pickBy() method
console.log(_.pickBy(obj, _.isLength));


Output:

{'password': 123456}

Example 2: In this example, we are using the _pickBy() method in which we are passing the _.isNumber() method which returns true if the value is a number.

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
// The source object
let obj = { 'x': 1, 'y': '2', 'z': 3 };
 
// Using the _.pickBy() method
console.log(_.pickBy(obj, _.isNumber));


Output:

{'x': 1, 'z': 3}
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments