Underscore.js _.juxt() Method

The _.juxt() method returns a function whose return value is an array of the results after calling each of the functions with the given arguments.

Syntax:

_.juxt( function1, function2, .., functionn );

Parameters: This method takes n functions containing the logic to return values.

Return Value: This method returns a function.

Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed.



underscore.js contrib library can be installed using npm install underscore-contrib –save.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
function firstG (val) {
    return val[0];
}
function F(val){
    return val[5];
}
function lastG (val) {
    return val[8];
}
  
// Defining function
var firstAndLastChars = _.juxt( firstG, F, lastG );
  
console.log(firstAndLastChars("GeeksforGeeks"));

chevron_right


Output:

[ 'G', 'f', 'G' ]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
function a() {
    return "a";
}
function b() {
    return "b";
}
function c() {
    return "c";
}
function d() {
    return "d";
}
  
  
// Defining function
var firstAndLastChars = _.juxt( a, b, c, d );
  
console.log(firstAndLastChars("GeeksforGeeks"));

chevron_right


Output:

[ 'a', 'b', 'c', 'd' ]

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.