Lodash _.conformTo() 方法
Lodash 是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、集合、字符串、对象、数字等。
_.conformsTo() 方法通过调用 source 的谓词属性与 object 的相应属性值来检查 object 是否符合 source,当 source 部分应用时,此方法等效于 _.conforms。
句法:
_.conformsTo(object, source)
参数:此方法接受上面提到的两个参数,如下所述:
- object:该方法要检查的对象。
- 来源:它是要符合的属性谓词的对象。
返回值:该方法返回要符合的属性谓词的对象。
示例一:这里使用 const _ = require('lodash') 来导入文件中的 lodash 库。
// Requiring the lodash library
const _ = require("lodash");
// Original array
var object = { 'p': 11, 'q': 9 };
// Using the _.conformsTo() method
let conform_data = _.conformsTo(object, {
'q': function
(n) { return n > 1; }
});
// Printing the output
console.log(conform_data);
输出:
true
示例 2:
// Requiring the lodash library
const _ = require("lodash");
// Original array
var object = { 'x': 1, 'y': 3 };
// Using the _.conformsTo() method
let conform_data = _.conformsTo(object, {
'q': function
(n) { return n > 3; }
});
// Printing the output
console.log(conform_data);
输出:
false
注意:此代码在普通 JavaScript 中不起作用,因为它需要安装库 lodash。