Friday, December 27, 2024
Google search engine
HomeLanguagesJavascriptCollect.js map() Method

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 rule or the condition to be applied on the collection. 

Return value :  Return the modified collection list.
 

Below examples illustrate the map() function in collect.js

Example 1: Here in this example, we take a collection and then using the map() function we apply the operation to the collection.

Javascript




// It is used to import collect.js library
const collect = require('collect.js');
  
const num = [1, 2, 3, 4, 5];
  
const data = collect(num);
  
const x = data.map(e => e * 3);
  
console.log(x.all());


Output: 

[ 3, 6, 9, 12, 15 ]

Example 2: Same as above example but applying a different operation to perform.

Python3




//It is used to import collect.js library
  
const collect = require('collect.js');
  
const num = [10 ,20 ,30 ,40, 50];
  
const data = collect(num);
  
const x = data.map(e => e / 10);
  
console.log(x.all());


Output: 

[1 , 2, 3 ,4 ,5]

Reference: https://collect.js.org/api/map.html

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