Lodash - matchesProperty 方法

语法

_.matchesProperty(source)

创建一个函数,对给定对象路径上的值与 srcValue 进行部分深度比较,如果对象值相等则返回 true,否则返回 false。

参数

  • path (Array|string) − 要获取的属性的路径。

  • srcValue (*) − 要匹配的值。

输出

  • (Function) −返回新的 spec 函数。

示例

var _ = require('lodash');
var objects = [
   { 'a': 1, 'b': 2, 'c': 3 },
   { 'a': 4, 'b': 5, 'c': 6 }
];
 
var result = _.find(objects, _.matchesProperty('a', 4));

console.log(result);

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

命令

\>node tester.js

输出

{ a: 4, b: 5, c: 6 }

lodash_util.html