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 this binding and arguments of the created function.
参数
pairs (Array) − The predicate-function pairs.
输出
(Function) − Returns the new composite function.
实例
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
输出
matches A
matches B
no match