Skip to content
Related Articles
Open in App
Not now

Related Articles

Lodash _.fix() Method

Improve Article
Save Article
  • Last Updated : 18 Sep, 2020
Improve Article
Save Article

The Lodash _.fix() method fixes the arguments to a function based on the parameter template defined by the presence of values and the _ placeholder.

The function takes a parameter that is replaced by _ in given values.

Syntax:

_.fix( fun, [values] )

Parameters: This method takes a single parameter as listed above and discussed below.

  • fun: This parameter holds the given function.
  • values: The values passed to the fun.

Return Value: It returns a new 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

Example 1: 

Javascript




// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function
function mul(a, b, c, d){
    return a*b*c*d;
}
  
// Making curried function
var gfgFunc = _.fix(mul, 1, 2, 3, _);
  
// 8 is replaced by _ in given values
console.log("Multiplication is :", gfgFunc(8));

Output:

Multiplication is : 48

Example 2:

Javascript




// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function
function add(a, b, c){
    return a + b + c;
}
  
// Making fixed function
var gfgFunc = _.fix(add, 1, 2, _);
  
console.log("Addition is :", gfgFunc(10));

Output: 

Addition is : 13

Example 3:

Javascript




// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function
function fun(str){
    return str;
}
  
// Making fixed function
var gfgFunc = _.fix(fun, _);
  
console.log("Coding Platform :",
      gfgFunc("GeeksforGeeks"));

Output: 

Coding Platform : GeeksforGeeks

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!