Open In App

Underscore.js _.unsplat() Method

Last Updated : 31 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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


Similar Reads

Lodash _.unsplat() Method
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 takin
2 min read
Underscore.js _.iterators.unfoldWithReturn() Method
With the help of _.iterators.unfoldWithReturn() method, we can get two values from iteration function where unary function is expected to return two values whenever function is invoked by using this method. Syntax: _.iterators.unfoldWithReturn(seed, unaryFn) Return Value: It returns the two values from iteration function. Note: To execute the below
1 min read
Underscore.js _.hasPath() Method
The _.hasPath() method returns a boolean indicating whether there is a property at the path described by the keys given. Keys may be given as an array or as a dot-separated string. Syntax: _.hasPath( Object_name, key_string|array ); Parameters: Object_name: Object from which the value is to be searched.key_string | array: Given dot-separated string
2 min read
Underscore.js _.cons() Method
The _.cons() Method is used to construct a new array by taking some element and putting it at the front of another array or element. Syntax: _.cons(element, Array_or_element); Parameters: element: It is the element that gets put in the front to construct a new Array.Array_or_element: It is the second parameter used to construct an array. Return Val
2 min read
Underscore.js _.iterateUntil() Method
The _.iterateUntil() Method takes two functions, one is used as a result generator, other function used as a stop-check and a seed value say n and returns an array of each generated result. The operation of _.iterateUntil() method is such that the result generator is passed with the seed value at the start and each subsequent result which will cont
2 min read
Underscore.js _.cycle() Method
The _.cycle() Method takes an integer value and an array which is then used to build an array containing the number of iterations through the given array, string end-to-end. A new array created contains the given array number of given times. Syntax: _.cycle(integer, array); Parameters: integer: The number of times the given array is iterated.array:
1 min read
Underscore.js _.interpose() Method
The _.interpose() Method takes an array and an element and returns a new array with the given element inserted in between every element of the original array. Syntax: _.interpose(array, element) Parameters: array: The array in which an element is to be inserted.element: The element to be inserted between every other element. Return Value: This meth
2 min read
Underscore.js _.chunkAll() Method
The _.chunkAll() Method is similar to _.chunk() method except for the following that, _.chunkAll() method will never drop short chunks from the end. It also takes an array and a number to make chunks and chunked array. syntax: _.chunkAll(array, number); or _.chunkAll(array, number, partitions); Parameters: array: The array to be split.number: The s
2 min read
Underscore.js _.iterators.List() Method
With the help of _.iterators.List() method, we can get the iterator which when called up gives the next value of the List by using this method. Syntax : _.iterators.List(array:Array) Parameters value: This method accepts a single parameter as mentioned above and described below: array: It holds the array where the method will be applied. Return: Re
1 min read
Underscore.js _.frequencies() Method
The _.frequencies() method takes an array and returns a mapping object whose keys are the values of the array's elements and values are counts of that key appeared in that array. Syntax: _.frequencies( array ); Parameters: This method accepts a single parameter as mentioned above and described below: array: The given array from which mapping is to
2 min read