Lodash _.complement() 方法
Lodash _.complement()方法返回一个反转给定谓词函数意义的函数。
句法:
_.complement( function );
参数:此方法接受上面列出并在下面讨论的单个参数。
- 函数:此参数谓词函数定义包含返回逻辑。
返回值:此方法返回一个函数。
注意:要执行以下示例,您必须使用此命令提示符安装lodash-contrib库并执行以下命令。
npm install lodash-contrib
下面的示例说明了 JavaScript 中的 Lodash _.complement() 方法:
示例 1:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
function gfgFun (x) {
return x>=2 ;
}
var comp = _.complement(gfgFun);
var x=1000;
console.log("Without Complement Function:",gfgFun(x))
console.log("With Complement Function:",comp(x));
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
function gfgFun (x) {
return x=="Geeks" ;
}
var comp = _.complement(gfgFun);
var x="GeeksforGeeks";
console.log("Without Complement Function:",gfgFun(x))
console.log("With Complement Function:",comp(x));
输出:
Without Complement Function: true
With Complement Function: false
示例 2:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
function gfgFun (x) {
return x=="Geeks" ;
}
var comp = _.complement(gfgFun);
var x="GeeksforGeeks";
console.log("Without Complement Function:",gfgFun(x))
console.log("With Complement Function:",comp(x));
输出:
Without Complement Function: false
With Complement Function: true