Lodash - isMatchWith 方法

语法

_.isMatchWith(object, source, [customizer])

此方法与 _.isMatch 类似,不同之处在于它接受调用来比较值的 customizer。如果 customizer 返回未定义,则由该方法处理比较。customizer 使用五个参数调用:(objValue、srcValue、index|key、object、source)。

参数

  • object (Object) − 要检查的对象。

  • source (Object) − 要匹配的属性值的对象。

  • [customizer] (Function) −自定义比较的函数。

输出

  • (boolean) − 如果对象匹配则返回 true,否则返回 false。

示例

var _ = require('lodash');

function isGreeting(value) {
   return /^h(?:i|ello)$/.test(value);
}
function customizer(objValue, srcValue) {
   if (isGreeting(objValue) && isGreeting(srcValue)) {
      return true;
   }
}
var object = { 'greeting': 'hello' };
var source = { 'greeting': 'hi' };
 
console.log(_.isMatchWith(object, source, customizer));

将上述程序保存在tester.js中。运行以下命令执行该程序。

命令

\>node tester.js

输出

true

lodash_lang.html