Lodash - cond方法



語法

_.cond(pairs)

建立一個函式,該函式迭代對,並呼叫第一個謂詞的相應函式以返回真值。謂詞-函式對使用建立的函式的 this 繫結和引數進行呼叫。

引數

  • pairs (陣列) − 謂詞-函式對。

輸出

  • (函式) − 返回新的複合函式。

示例

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' }));

將上述程式儲存在 tester.js 中。執行以下命令以執行此程式。

命令

\>node tester.js

輸出

matches A
matches B
no match
lodash_util.htm
廣告
© . All rights reserved.