Collect.js make() Function

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 moduleconst collect = require('collect.js')// User defined collectionvar myCollection = [1,2,3,4,5]function make(items = []) { return new this.constructor(items);}// Creating collection objectconst collection = collect(myCollection);// Printing collectionconsole.log(collection.all());// Make Function callconsole.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 moduleconst collect = require('collect.js')// User defined collectionvar myCollection = ['Monday', 'Tuesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']// Function definitionfunction make(items = []) { return new this.constructor(items);}// Creating collection objectconst collection = collect(myCollection);// Make Function callconsole.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
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek 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!



