Lodash _.spread() Method
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
// Requiring lodash library const _ = require( 'lodash' ); // Using the _.spread() method with its parameter let write = _.spread( function (author, portal) { return author + ' writes for ' + portal + '!' ; }); // Calling write with its values console.log(write([ 'Nidhi' , 'w3wiki' ])); |
Output:
Nidhi writes for w3wiki!
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
// Requiring lodash library const _ = require( 'lodash' ); // Using the _.spread() method with its parameter let addition = _.spread( function (x, y) { return x + y; }); // Calling addition with its values console.log(addition([56, 44])); |
Output:
100
Contact Us