Open In App

Collect.js macro() Method

Last Updated : 26 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The macro() method is used to register a custom method. This method used the custom function.

Syntax:

collect(array).macro()

Parameters: The collect() method takes one argument that is converted into the collection and then macro() method is applied on it.

Return Value: This method returns the collection keys.

Below example illustrate the macro() method in collect.js:

Example 1:

Javascript




const collect = require('collect.js');
  
collect().macro('even', function () {
    return this.map(element => element % 2 == 0);
});
  
const arr = [2, 3, 5, 6, 8, 9, 10, 12, 13, 16];
  
const collection = collect(arr);
  
console.log(collection.even());


Output:

Collection {
  items: [
    true, false, false,
    true, true,  false,
    true, true,  false,
    true
  ]
}

Example 2:

Javascript




const collect = require('collect.js');
  
collect().macro('uppercase', function () {
    return this.map(element => element.toUpperCase());
});
  
const arr = ['gfg', 'geeks', 'GeeksforGeeks'];
  
const collection = collect(arr);
  
console.log(collection.uppercase());


Output:

Collection { items: [ 'GFG', 'GEEKS', 'GEEKSFORGEEKS' ] }

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads