_.cond

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

Since

4.0.0

Arguments

  1. pairs (Array): The predicate-function pairs.

Returns

(Function): Returns the new composite function.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var func = _.cond([
  [_.matches({ 'a': 1 }),           _.constant('matches A')],
 
  [_.conforms({ 'b': _.isNumber }), _.constant('matches B')],
 
  [_.stubTrue,                      _.constant('no match')]
]);
  
 
func({ 'a': 1, 'b': 2 });
// => 'matches A'
  
 
func({ 'a': 0, 'b': 1 });
// => 'matches B'
  
 
func({ 'a''1''b''2' });
// => 'no match'
doc_Lodash
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.