The Wayback Machine - https://web.archive.org/web/20240118171344/https://www.geeksforgeeks.org/lodash-_-cond-method/
Open In App
Related Articles

Lodash _.cond() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Lodash _.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:

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

Return Value:

The predicate-function pairs.

Example 1: In this example, we are checking whether the given value is matching to the given condition and returning the result accordingly or not.

Javascript




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


Output:

"Matches Geeks"

Example 2: In this example, we are checking whether the given value is matching to the given condition and returning the result accordingly or not.

Javascript




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


Output:

"Matches For"
"No Match"

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks 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!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 03 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials