Tuesday, December 31, 2024
Google search engine
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

Most Popular

Recent Comments