Open In App

Collect.js mapSpread() Method

Last Updated : 26 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Javascript




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:

Javascript




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
]

Similar Reads

Collect.js toArray() Method
The toArray() method is used to convert the given collection into a normal array. An array containing the values will be returned if the collection is an object. Syntax: collect(array).toArray() Parameters: The collect() method takes one argument that is converted into the collection and the toArray() method is applied to it. Return Value: This met
1 min read
Collect.js replace() Method
The replace() method is used to replace the elements of the original collection that match the given string or numeric keys. If the key given in the object matches with a key in the collection, then its value is replaced, otherwise, if the key does not match, then it is added to the collection. Syntax: collect(array).replace(object) Parameters: The
1 min read
Collect.js reject() Method
The reject() method is used to filter the given collection of elements using the given callback function. If the callback function returns true, then the element is removed from the resulting collection, otherwise, it is not removed. Syntax: collect(array).reject(callback) Parameters: The collect() method takes one argument that is converted into t
1 min read
Collect.js | avg() Method
Collect.js is a fluent and convenient wrapper for working with arrays and objects. The JavaScript array is first transformed into a collection and then the function is applied to the collection. The avg() method returns the average of all the items in a collection. Installation: Collect.js can be installed via NPM: npm install --save collect.js You
2 min read
Collect.js | combine() Method
Collect.js is a fluent and convenient wrapper for working with arrays and objects. The JavaScript array is first transformed into a collection and then the function is applied to the collection.The combine() method combines two different arrays with keys-values. Installation: Collect.js can be installed via NPM: npm install --save collect.jsYou can
1 min read
Collect.js | crossJoin() Method
Collect.js is a fluent and convenient wrapper for working with arrays and objects. The JavaScript array is first transformed into a collection and then the function is applied to the collection. The crossJoin() method cross joins the collection with an array or collection and return all possible permutations between them. Installation: Collect.js c
1 min read
Collect.js | collapse() Method
Collect.js is a fluent and convenient wrapper for working with arrays and objects. The JavaScript array is first transformed into a collection and then the function is applied to the collection. The collapse() method convert collection of arrays into a single flat array. Installation: Collect.js can be installed via NPM: npm install --save collect.
2 min read
Collect.js | all() Method
The all() method is used to return the underlying array or object represented by the collection. The JavaScript array is first transformed into a collection and then the function is applied to the collection. Syntax: collect(array).all() Parameters: The method does not accept any parameter. Return Value: Returns an array or object. Below examples i
1 min read
Collect.js map() Method
The map() function iterates through a collection and pass each value to callback.In JavaScript, the array is first converted to a collection and then the function is applied to the collection.Syntax: data.map(rule) Parameters: This function accepts a single parameter as mentioned above and described below: rule: This parameter holds the operation r
1 min read
Collect.js each() Method
The each() function iterates over the items in the collection and passes each item to a callback. In JavaScript, the array is first converted to a collection and then the function is applied to the collection. Syntax: data.each(item) Parameters: This function accept a single parameter as mentioned above and described below: item: This parameter hol
1 min read