📜  Lodash _.matchesProperty() 方法

📅  最后修改于: 2022-05-13 01:58:10.285000             🧑  作者: Mango

Lodash _.matchesProperty() 方法

Lodash 是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、字符串、对象、数字等。 matchesProperty方法创建一个函数,该函数在给定对象的路径中的值与 srcValue 之间执行部分深度比较,如果对象值相等则返回 true,否则返回 false。

句法:

_.matchesProperty(path, srcValue)

参数:此方法接受上面提到的两个参数,如下所述:

  • path: [Array/ 字符串] 要获取的属性的路径。
  • srcValue:要匹配的值。

返回: [函数] 返回新指定的函数。

示例 1:

// Requiring the lodash library  
const _ = require("lodash");    
   
// Using _.matchesProperty() method
var geek = [
  { 'java': 3, 'python': 5, 'js': 7 },
  { 'java': 4, 'python': 2, 'js': 6 }
];
    
let gfg = _.find(geek, _.matchesProperty('java', 4));
   
// Storing the Result
console.log(gfg)

注意:这里使用 const _ = require('lodash') 来导入文件中的 lodash 库。

输出:

Object {java: 4, js: 6, python: 2}

示例 2:

// Requiring the lodash library  
const _ = require("lodash");    
   
// Using _.matchesProperty() method
var geek = [
  { 'a': 1, 'b': 2, 'c': 3 },
  { 'a': 4, 'b': 5, 'c': 6 },
   { 'a': 8, 'b': 7, 'c': 9 }
];
    
gfg = _._.find(geek, _.matchesProperty('a', 4));
  
// Storing the Result
console.log(gfg)

注意:这里使用 const _ = require('lodash') 来导入文件中的 lodash 库。

输出:

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