Open In App

Collect.js make() Function

Last Updated : 26 Nov, 2022
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Collect.js is a fluent and convenient wrapper for working with arrays and objects. The make() function creates a new collection instance.

Installation: Install the Collect.js module using the following command:

npm install --save collect.js

Syntax:  

collection.make(user collection)

Parameters: This function takes only one parameter i.e. the user collection which is the collection defined by the user. 

Return Value: This function returns the new collection object.

Example 1: Filename-index.js 

JavaScript
// Requiring module
const collect = require('collect.js')

// User defined collection
var myCollection = [1,2,3,4,5]

function make(items = []) {
    return new this.constructor(items);
}

// Creating collection object
const collection = collect(myCollection);

// Printing collection
console.log(collection.all());

// Make Function call
console.log(make([1,2,3]))

Run the index.js file using the following command:

node index.js

Output:

[ 1, 2, 3, 4, 5 ]
[ 1, 2, 3 ]

Example 2: Filename-index.js 

JavaScript
// Requiring module
const collect = require('collect.js')

// User defined collection
var myCollection = ['Monday', 'Tuesday', 'Thursday',
    'Friday', 'Saturday', 'Sunday']

// Function definition
function make(items = []) {
    return new this.constructor(items);
}

// Creating collection object
const collection = collect(myCollection);

// Make Function call
console.log(make(collection))

Run the index.js file using the following command:

node index.js

Output:

Collection {
  items: [ 'Monday', 'Tuesday', 'Thursday', 
    'Friday', 'Saturday', 'Sunday' ]
}

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


Similar Reads