Lodash _.conforms() 方法
Lodash是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、字符串、对象、数字等。
Util 的_.conforms() 方法用于创建一个函数,该函数使用声明对象的类似属性值调用源的谓词属性,如果所有谓词都为 true,则返回 true,否则返回 false。
句法:
_.conforms(source)
参数:此方法接受单个参数,如下所述:
源(Object):要符合的属性谓词的对象。
返回值:该方法返回新指定的函数。
示例 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