Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptCollect.js make() Function

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 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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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!

RELATED ARTICLES

Most Popular

Recent Comments