The Lodash _.unsplat() method takes a function expecting an array as its last argument and returns a function which works identically, but takes a list of trailing arguments instead of an array.
Syntax:
_.unsplat( function );
Parameters: This method accepts a single parameter as mentioned above and discussed below:
- function: Original function taking its last arguments as an array.
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 _.unsplat() method in JavaScript:
Example 1:
Javascript
// Defining lodash contrib variable var _ = require('lodash-contrib');Â Â Â function g (val, arr) { Â Â Â Â return val+" : "+arr; } Â Â var gfgFunc = _.unsplat(g); Â Â console.log(gfgFunc("a", 10, 20, 30, 40)) |
Output:
a : 10, 20, 30, 40
Example 2:Â
Javascript
// Defining lodash contrib variable var _ = require('lodash-contrib');Â Â Â function g (arr) { Â Â Â Â return arr; } Â Â var gfgFunc = _.unsplat(g); Â Â console.log(gfgFunc(100, 200, 300, 400)) |
Output:
[ 100, 200, 300, 400 ]
Example 3:Â
Javascript
// Defining lodash contrib variable var _ = require('lodash-contrib');Â Â Â function g (val,arr) { Â Â Â Â return arr.join(val); } Â Â var gfgFunc = _.unsplat(g); Â Â console.log(gfgFunc(" : ", "neveropen", "Computer Science Portal for Geeks")) |
Output:
neveropen : Computer Science Portal for Geeks
