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, .., function );
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: In this example, we will see the use of the _.juxt() method
javascript
// Defining underscore contrib variableconst _ = require('underscore-contrib');Â
function firstG(val) {Â Â Â Â return val[0];}function F(val) {Â Â Â Â return val[5];}function lastG(val) {Â Â Â Â return val[8];}Â
// Defining functionlet firstAndLastChars = _.juxt(firstG, F, lastG);Â
console.log(firstAndLastChars("neveropen")); |
Output:
[ 'G', 'f', 'G' ]
Example 2: In this example, we will see the use of the _.juxt() method
javascript
// Defining underscore contrib variableconst _ = require('underscore-contrib');Â
function a() {Â Â Â Â return "a";}function b() {Â Â Â Â return "b";}function c() {Â Â Â Â return "c";}function d() {Â Â Â Â return "d";}Â
Â
// Defining functionlet firstAndLastChars = _.juxt(a, b, c, d);Â
console.log(firstAndLastChars("neveropen")); |
Output:
[ 'a', 'b', 'c', 'd' ]
