_.matchesProperty(path, srcValue)

_.matchesProperty() 方法创建一个深度比较的方法,用来检查对象的指定路径上的值是否与给定值相等。

参数

  • path (Array|string): 要检查的属性路径。
  • srcValue (*): 要匹配的值。

返回值

(Function): 返回一个新的断言函数。

示例

const users = [
  { user: "barney", age: 36 },
  { user: "fred", age: 40 },
];

// 创建一个用于匹配的断言函数
const matchesPropFunc = _.matchesProperty("user", "fred");

// 使用断言函数测试对象是否匹配条件
console.log(matchesPropFunc(users[0]));
// => false

console.log(matchesPropFunc(users[1]));
// => true

在这个示例中,_.matchesProperty() 创建了一个用于匹配的断言函数,该函数会检查对象的 user 属性是否与给定值相等。