- Lodash Tutorial
- Lodash - Home
- Lodash - Overview
- Lodash - Environment Setup
- Lodash - Array
- Lodash - Collection
- Lodash - Date
- Lodash - Function
- Lodash - Lang
- Lodash - Math
- Lodash - Number
- Lodash - Object
- Lodash - Seq
- Lodash - String
- Lodash - Util
- Lodash - Properties
- Lodash - Methods
- Lodash Useful Resources
- Lodash - Quick Guide
- Lodash - Useful Resources
- Lodash - Discussion
Lodash - cond method
Syntax
_.cond(pairs)
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 this binding and arguments of the created function.
Arguments
pairs (Array) − The predicate-function pairs.
Output
(Function) − Returns the new composite function.
Example
var _ = require('lodash'); var check = _.cond([ [_.matches({ 'a': 1 }), _.constant('matches A')], [_.conforms({ 'b': _.isNumber }), _.constant('matches B')], [_.stubTrue, _.constant('no match')] ]); console.log(check({ 'a': 1, 'b': 2 })); console.log(check({ 'a': 0, 'b': 1 })); console.log(check({ 'a': '1', 'b': '2' }));
Save the above program in tester.js. Run the following command to execute this program.
Command
\>node tester.js
Output
matches A matches B no match
lodash_util.htm
Advertisements
To Continue Learning Please Login
Login with Google