Lodash _.complement() Method
Last Updated :
18 Sep, 2020
The Lodash _.complement() method returns a function that reverses the sense of a given predicate-function.
Syntax:
_.complement( function );
Parameters: This method accepts a single parameter as listed above and discussed below.
- function: This parameter predicate function defined containing the returning logic.
Return Value: This method returns a function.
Note: To execute the below examples, you have to install the lodash-contrib library by using this command prompt and execute the following command.
npm install lodash-contrib
Below examples illustrate the Lodash _.complement() method in JavaScript:
Example 1:
Javascript
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));
|
Output:
Without Complement Function: true
With Complement Function: false
Example 2:
Javascript
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));
|
Output:
Without Complement Function: false
With Complement Function: true
Share your thoughts in the comments
Please Login to comment...