Skip to content
Related Articles

Related Articles

Improve Article

Underscore.js _.unsplat() Method

  • Last Updated : 31 Aug, 2020

The _.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: 

  • function: Original function taking its last arguments as an array.

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:

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
function g (val, arr) {
    return val + " : " + arr;
}
  
var gfgFunc = _.unsplat(g);
  
console.log(gfgFunc("a", 1, 2, 3, 4))

Output:

a : 1, 2, 3, 4

Example 2:

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
function g (arr) {
    return arr;
}
  
var gfgFunc = _.unsplat(g);
  
console.log(gfgFunc(1, 2, 3, 4))

Output:

[ 1, 2, 3, 4 ]

Example 3:

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
function g (val, arr) {
    return arr.join(val);
}
  
var gfgFunc = _.unsplat(g);
  
console.log(gfgFunc(" : ", "GeeksforGeeks"
      "Computer Science Portal for Geeks"))

Output:

GeeksforGeeks : Computer Science Portal for Geeks



My Personal Notes arrow_drop_up
Recommended Articles
Page :