當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Lodash _.matchesProperty()用法及代碼示例

Lodash是一個JavaScript庫,可在underscore.js頂部使用。 Lodash幫助處理數組,字符串,對象,數字等。_.matchesProperty方法創建一個函數,該函數在給定對象的路徑到srcValue的值之間執行部分深度比較,如果對象值相等,則返回true,否則是錯誤的。

用法:

_.matchesProperty(path, srcValue)

參數:此方法接受上麵提到和下麵描述的兩個參數:

  • path:[Array /string]要獲取的屬性的路徑。
  • 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}




相關用法


注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 Lodash _.matchesProperty() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。