Open In App

Lodash _.spread() Method

Last Updated : 03 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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', '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
// 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

Similar Reads