Lodash _.spread() Method
Last Updated :
03 Nov, 2023
Lodash _.spread() method is used to create a function that calls the given function as a parameter using this binding of the create function, along with an array of arguments. It is based on the spread operator in JavaScript.
Syntax:
_.spread(func, start);
Parameters:
- func: It is the function that is used to spread arguments over.
- start: It is the start position of the spread. It is an optional parameter. The default value is 0.
Return Value:
This method returns the new function.
Example 1: In this example, we are passing two strings in a function and printing the result in the console by the use of the lodash _.spread() method.
Javascript
const _ = require( 'lodash' );
let write = _.spread( function (author, portal) {
return author + ' writes for ' + portal + '!' ;
});
console.log(write([ 'Nidhi' , 'GeeksforGeeks' ]));
|
Output:
Nidhi writes for GeeksforGeeks!
Example 2: In this example, we are passing two numbers in a addition function and printing the result in the console by the use of the lodash _.spread() method.
Javascript
const _ = require( 'lodash' );
let addition = _.spread( function (x, y) {
return x + y;
});
console.log(addition([56, 44]));
|
Output:
100
Share your thoughts in the comments
Please Login to comment...