The Lodash _.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 accepts n functions containing the logic to return values.
Return Value: This method returns a 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
Below examples illustrate the Lodash _.juxt() method in JavaScript:
Example 1:
Javascript
// Defining lodash contrib variablevar _ = require('lodash-contrib'); Â
function firstG (val) {Â Â Â Â return val[0];}function F(val){Â Â Â Â return val[5];}function lastG (val) {Â Â Â Â return val[8];}Â
// Defining functionvar firstAndLastChars = _.juxt( firstG, F, lastG );Â
console.log(firstAndLastChars("neveropen")); |
Output:
[ 'G', 'f', 'G' ]
Example 2:Â
Javascript
// Defining lodash contrib variablevar _ = require('lodash-contrib'); Â
function a() {Â Â Â Â return "a";}function b() {Â Â Â Â return "b";}function c() {Â Â Â Â return "c";}function d() {Â Â Â Â return "d";}Â
Â
// Defining functionvar abcd = _.juxt( a, b, c, d );Â
console.log(abcd()); |
Output:
[ 'a', 'b', 'c', 'd' ]
