The Lodash _.binary() method returns a new function that accepts only two arguments and passes these arguments to the given function. Additional arguments are discarded.
Syntax:
_.binary( fun )
Parameters: This method takes a single parameter as listed above and discussed below.
- fun: This is the given function.
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 fun(){ Â Â Â Â var mul = 1; Â Â Â Â Â for (var i = 0; i < arguments.length; i++) { Â Â Â Â Â Â Â Â Â mul = mul * arguments[i]; Â Â Â Â Â } Â Â Â Â Â return mul; } Â Â var gfgFunc = _.binary(fun); Â Â console.log("Multiplication is :", gfgFunc(2,23)); |
Output:
Multiplication is : 46
Example 2:
Javascript
// Defining lodash contrib variable var _ = require('lodash-contrib');Â Â Â // Function function fun(){ Â Â Â Â var mul = 1; Â Â Â Â Â for (var i = 0; i < arguments.length; i++) { Â Â Â Â Â Â Â Â Â mul = mul * arguments[i]; Â Â Â Â Â } Â Â Â Â Â return mul; } Â Â var gfgFunc = _.binary(fun); Â Â // Only operates for first two parameters console.log("Multiplication is :", gfgFunc(2,23,10)); |
Output:Â
Multiplication is : 46
Example 3:
Javascript
// Defining lodash contrib variable var _ = require('lodash-contrib');Â Â Â // Function function fun(){ Â Â Â Â return arguments; } Â Â var gfgFunc = _.binary(fun); Â Â // Only operates for first two parameters console.log("Arguments are :", Â Â Â Â gfgFunc('arg1', 'arg2', 'arg3', 'arg4')); |
Output: Extra arguments are discarded.
Arguments are : [Arguments] { '0': 'arg1', '1': 'arg2' }
