Lodash _.splat() Method
Last Updated :
18 Sep, 2020
The _.splat() method takes a function that accepts one or more arguments and hence returns a function that takes an array and uses its elements as the arguments to the original function.
Syntax:
_.splat( function );
Parameters: This method accepts a single parameter as mentioned above and discussed below:
- function: original function containing arguments.
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 _.splat() method in JavaScript:
Example 1:
Javascript
var _ = require( 'lodash-contrib' );
function addNum (a, b) {
return a + b;
}
var listTwoNamesFromArray = _.splat(addNum);
console.log( listTwoNamesFromArray([1, 10]) );
|
Output:
11
Example 2:
Javascript
var _ = require( 'lodash-contrib' );
function addNum (a, b) {
return a + " : " + b;
}
var listTwoNamesFromArray = _.splat(addNum);
console.log( listTwoNamesFromArray([ "GeeksforGeeks" ,
"Computer Science Portal for Geeks" ]) );
|
Output:
GeeksforGeeks : Computer Science Portal for Geeks
Share your thoughts in the comments
Please Login to comment...