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


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

Lodash是一個JavaScript庫,可在underscore.js頂部使用。 Lodash幫助處理數組,字符串,對象,數字等。

Util的_.conforms()方法用於創建一個函數,該函數使用指定對象的相似屬性值調用源的謂詞屬性,如果所有謂詞為true,則返回true,否則返回false。

用法:

_.conforms(source)

參數:此方法接受單個參數,如下所述:

  • source(對象):符合條件的屬性謂詞的對象。



返回值:此方法返回新的指定函數。

範例1:

// Requiring the lodash library 
const _ = require('lodash'); 
  
// Initializing an object 
var object = [ 
  { 'x':2, 'y':3 }, 
  { 'x':5, 'y':6 } 
]; 
  
// Calling _.conforms() function with its parameter 
let newfunc = _.filter(object, _.conforms({ 'y':function(n) {  
return n > 3; } })); 
  
// Displays output 
console.log(newfunc);

輸出:

[ { x:5, y:6 } ]

範例2:

// Requiring the lodash library 
const _ = require('lodash'); 
  
// Initializing an object 
var object = [ 
  { 'GfG':13, 'geeksforgeeks':5 }, 
  { 'GfG':7, 'geeksforgeeks':4 } 
]; 
  
// Calling _.conforms() function with its parameter 
let newfunc = _.filter(object, _.conforms({ 'GfG':function(n) {  
return n > 13; } })); 
  
// Displays output 
console.log(newfunc);

輸出:

[]

在此,由於不滿足所述條件,因此不返回任何內容。

參考:https://lodash.com/docs/4.17.15#conforms




相關用法


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