HomeLanguagesJavascriptLodash _.splat() Method

Lodash _.splat() Method

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




// Defining lodash contrib variable
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




// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
function addNum (a, b) {
    return a +" : "+ b;
}
  
var listTwoNamesFromArray = _.splat(addNum);
  
console.log( listTwoNamesFromArray(["neveropen", 
    "Computer Science Portal for Geeks"]) );


Output:

neveropen : Computer Science Portal for Geeks
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS