Open In App

Collect.js mapSpread() Method

The mapSpread() method is used to iterate over the given collection items and pass each nested collection item value into the given callback.

Syntax:



collect(array).mapSpread(callback)

Parameters: The collect() method takes one argument that is converted into the collection and then mapSpread() method is applied on it. The mapSpread() method holds the callback function as parameter.

Return Value: This method returns collection items according to given callback function.



Below example illustrate the mapSpread() method in collect.js:

Example 1:




const collect = require('collect.js');
  
const arr = ['a', 'b', 'c'];
  
const collection = collect(arr);
  
const sequence = collection.mapSpread(
    element => element.toUpperCase());
  
console.log(sequence.all());

Output:

[ 'A', 'B', 'C' ]

Example 2:




const collect = require('collect.js');
  
const arr = [2, 4, 5, 6, 7, 8, 10, 11];
  
const collection = collect(arr);
  
const sequence = collection.mapSpread(
    element => element % 2 == 0);
  
console.log(sequence.all());

Output:

[
  true, false,
  true, false,
  true, false,
  true, false
]
Article Tags :