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
const _ = require( "lodash" );
let func1 = _.cond([
[_.matches({ 'geeks' : 1 }),
_.constant( 'Matches Geeks' )],
[_.conforms({ 'for' : _.isNumber }),
_.constant( 'Matches For' )],
[_.stubTrue, _.constant( 'no match' )]
]);
gfg = func1({ 'geeks' : 1, 'b' : 2 });
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
const _ = require( "lodash" );
let func2 = _.cond([
[_.matches({ 'geeks' : 1 }),
_.constant( 'Matches Geeks' )],
[_.conforms({ 'for' : _.isNumber }),
_.constant( 'Matches For' )],
[_.stubTrue, _.constant( 'No Match' )]
]);
gfg1 = func2({ 'geeks' : 0, 'for' : 1 });
gfg2 = func2({ 'geeks' : '1' , 'for' : '2' });
console.log(gfg1);
console.log(gfg2);
|
Output:
"Matches For"
"No Match"
Last Updated :
03 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...