Skip to content

cond

js
_.cond(pairs)

创建一个迭代 pairs 并调用第一个谓词的相应函数以返回真值的函数。使用 this 绑定和创建的函数的参数调用谓词函数对。

¥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 (数组):要检查的谓词。

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

返回

¥Returns

(函数):返回新的复合函数。

¥(Function): Returns the new composite function.

示例

¥Example

js
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'

Lodash v4.17 中文网 - 粤ICP备13048890号