How to use the spread function from lodash

Find comprehensive JavaScript lodash.spread code examples handpicked from public code repositorys.

lodash.spread is a function that takes a function with multiple arguments and returns a new function that can spread an array of arguments into the original function.

359
360
361
362
363
364
365
366
367
368
module.exports.sortedUniqBy        = _.sortedUniqBy;
module.exports.splat               = _.splat;
module.exports.split               = _.split;
module.exports.splitAt             = _.splitAt;
module.exports.splitWith           = _.splitWith;
module.exports.spread              = _.spread;
module.exports.startCase           = _.startCase;
module.exports.startsWith          = _.startsWith;
module.exports.strContains         = _.strContains;
module.exports.stripTags           = _.stripTags;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

347
348
349
350
351
352
353
354
355
356
357
358
359
console.log(rearg(1, 2, 3)); // => [3, 1, 2]


const rest = _.rest((a, b, ...c) => [a, b, c]);
console.log(rest(1, 2, 3, 4)); // => [1, 2, [3, 4]]


const spread = _.spread((a, b, c) => a + b + c);
console.log(spread([1, 2, 3])); // => 6


const throttle = _.throttle(() => console.log('hello'), 100);
throttle();
fork icon0
star icon4
watch icon0

+ 15 other calls in file

How does lodash.spread work?

lodash.spread is a function in the Lodash library that takes a function with multiple arguments and returns a new function that can spread an array of arguments into the original function. Here's how it works:

  1. You start by passing a function with multiple arguments to lodash.spread, which returns a new function that can be called with an array of arguments.

  2. When you call the new function with an array of arguments, lodash.spread will use the apply method to spread the array of arguments into the original function.

  3. The original function is called with the individual elements of the array as separate arguments.

  4. The result of the original function call is returned as the result of the new function.

Here is an example of using lodash.spread:

javascript
const spreadFn = lodash.spread((a, b, c) => a + b + c); console.log(spreadFn([1, 2, 3])); // Output: 6

In this example, we create a new function using lodash.spread that takes three arguments and returns their sum.

We then call the new function with an array of three numbers, which is spread into the original function using apply.

The original function adds up the three numbers and returns their sum, which is logged to the console.

Note that the lodash.spread function is often used in conjunction with other Lodash functions like _.map or _.reduce, which allow you to process arrays of values with a function that expects multiple arguments. By using lodash.spread, you can write these functions as if they take individual arguments, but still handle arrays of values as input.

237
238
239
240
241
242
243
244
245
246
247
var say1 = say('hello', 'fred', 'barney', 'pebbles');
console.log('say1--->', say1);
//say1---> hello fred, barney, & pebbles


//_.spread(func,[start=0])
var spreadSay = _.spread(function (who, what) {
    return who + ' says ' + what;
});
var spreadSay1 = say(['fred', 'hello']);
console.log('spreadSay1--->', spreadSay1);
fork icon0
star icon0
watch icon0

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
const lodash = require("lodash");

function sum(a, b, c) {
  return a + b + c;
}

const spreadFn = lodash.spread(sum);

console.log(spreadFn([1, 2, 3])); // Output: 6

In this example, we define a function sum that takes three arguments and returns their sum. We then create a new function using lodash.spread and pass sum as the argument. This creates a new function that can accept an array of values as its argument, which is then spread into sum. Finally, we call the new function with an array of three numbers [1, 2, 3], and it returns the sum of those numbers, which is 6. Note that we could also have achieved the same result without using lodash.spread like this: javascript Copy code

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)