Collect.js | map() Function

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());

chevron_right


Output: 

[ 3, 6, 9, 12, 15 ]

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

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

//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());

chevron_right


Output: 

[1 , 2, 3 ,4 ,5]

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

full-stack-img

My Personal Notes arrow_drop_up

Check this out nerdboinet

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : akhilsharma870

Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.