Monday, October 6, 2025
HomeLanguagesJavascriptLodash _.cond() Method

Lodash _.cond() 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 _.cond method creates a function that iterates over pairs and invokes the corresponding function of the first predicate to return truthy. The predicate-function pairs are invoked with the binding and arguments of the created function.

Syntax:

_.cond(pairs)

Parameters: This method accepts one parameter as mentioned above and described below:

  • pairs: [Array]The predicate-function pairs.

Return Value:  [Array]The predicate-function pairs.

Note : Here, const _ = require(‘lodash’) is used to import the lodash library in the file.

Example 1:

Javascript




// Requiring the lodash library  
const _ = require("lodash");
  
//  using _.cond() method
var func1 = _.cond([
    [_.matches({ 'neveropen': 1 }),
        _.constant('Matches Geeks')],
    [_.conforms({ 'for': _.isNumber }),
        _.constant('Matches For')],
    [_.stubTrue, _.constant('no match')]
]);
  
// Storing the Result
gfg = func1({ 'neveropen': 1, 'b': 2 });
  
// Printing the output  
console.log(gfg);


Output:

"Matches Geeks"

Example 2:

Javascript




// Requiring the lodash library  
const _ = require("lodash");
  
//  using _.cond() method
var func2 = _.cond([
    [_.matches({ 'neveropen': 1 }), 
        _.constant('Matches Geeks')],
    [_.conforms({ 'for': _.isNumber }), 
        _.constant('Matches For')],
    [_.stubTrue, _.constant('No Match')]
]);
  
// Storing Result 
gfg1 = func2({ 'neveropen': 0, 'for': 1 });
gfg2 = func2({ 'neveropen': '1', 'for': '2' });
  
// Printing the output  
console.log(gfg1);
console.log(gfg2)


Output:

"Matches For"
"No Match"
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

Dominic
32338 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6707 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6825 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6781 POSTS0 COMMENTS