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({ '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:
Javascript
// Requiring the lodash library const _ = require("lodash"); // using _.cond() method var 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"
Recommended Posts:
- Lodash _.method() Method
- Lodash _.sneq() Method
- Lodash _.toQuery() Method
- Lodash _.uniqWith() Method
- Lodash _.xorWith() Method
- Lodash | _.head() Method
- Lodash | _.remove() Method
- Lodash | _.pullAt() Method
- Lodash | _.pullAll() Method
- Lodash | _.pull() Method
- Lodash | _.nth() Method
- Lodash | _.takeRight() Method
- Lodash | _.take() Method
- Lodash | _.sortedLastIndex() Method
- Lodash | _.fromPairs() Method
- Lodash | _.differenceWith() Method
- Lodash | _.castArray() Method
- Lodash | _.cloneDeep() Method
- Lodash | _.clone() Method
- Lodash | _.sampleSize() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


