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