Lodash _.quaternary() Method

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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"));

chevron_right


Output:

Arguments are : [Arguments] {
  '0': 'first',
  '1': 'second',
  '2': 'third',
  '3': 'fourth'
}

Example 2:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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"));

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));

chevron_right


Output: 

Sum of first 4 arguments is : 400

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.