HomeLanguagesJavascriptCollect.js mapSpread() Method

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:

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
]
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!
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32734 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7116 POSTS0 COMMENTS
Nicole Veronica
12250 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12333 POSTS0 COMMENTS
Shaida Kate Naidoo
7228 POSTS0 COMMENTS
Ted Musemwa
7487 POSTS0 COMMENTS
Thapelo Manthata
7208 POSTS0 COMMENTS
Umr Jansen
7180 POSTS0 COMMENTS