Thursday, July 4, 2024
HomeLanguagesJavascriptLodash _.spread() Method

Lodash _.spread() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.

The _.spread() method is used to create a function that calls the given function as parameter using the 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: This method accept two parameters as mentioned above and described below:

  • 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:

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Using the _.spread() method with its parameter
var write = _.spread(function(author, portal) {
  return author + ' writes for ' + portal + '!';
});
  
// Calling write with its values
write(['Nidhi', 'neveropen']);


Output:

Nidhi writes for neveropen!

Example 2:

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Using the _.spread() method with its parameter
var addition = _.spread(function(x, y) {
  return x + y;
});
  
// Calling addition with its values
addition([56, 44]);


Output:

100
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!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments