The Lodash _.quaternary() returns a new function that accepts only four arguments and passes these arguments to the given function. Additional arguments are discarded.
Syntax:
_.quaternary( fun )
Parameters: This method takes a single parameter as listed above and discussed below.
- fun: This is the given function.
Return Value: This method 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(){ Â Â Â Â return arguments; } Â Â // Making quaternary function var gfgFunc = _.quaternary(fun); Â Â console.log( "Arguments are :" , gfgFunc( Â Â Â Â "first" , "second" , "third" , "fourth" )); |
Output:
Arguments are : [Arguments] { '0': 'first', '1': 'second', '2': 'third', '3': 'fourth' }
Example 2:
Javascript
// Defining lodash contrib variable var _ = require( 'lodash-contrib' );Â Â Â // Function function fun(){ Â Â Â Â return arguments; } Â Â // Making quaternary function var gfgFunc = _.quaternary(fun); Â Â // Rest arguments are excluded console.log( "Arguments are :" , gfgFunc( Â Â Â Â "a" , "b" , "c" , "d" , "e" , "f" )); |
Output:Â
Arguments are : [Arguments] { '0': 'a', '1': 'b', '2': 'c', '3': 'd' }
Example 3: In this example, we will add arguments but only the first 4 arguments will be using this method.
Javascript
// Defining lodash contrib variable var _ = require( 'lodash-contrib' );Â Â Â // Function function add(){ Â Â Â Â s = 0 Â Â Â Â for (i = 0; i < 4; i++){ Â Â Â Â Â Â Â Â s += arguments[i] Â Â Â Â } Â Â Â Â return s; } Â Â // Making quaternary function var gfgFunc = _.quaternary(add); Â Â // Rest arguments are excluded console.log( "Sum of first 4 arguments is :" , Â Â Â Â gfgFunc(100, 100, 100, 100, 100)); |
Output:Â
Sum of first 4 arguments is : 400