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
const collect = require( 'collect.js' )
var myCollection = [1,2,3,4,5]
function make(items = []) {
return new this .constructor(items);
}
const collection = collect(myCollection);
console.log(collection.all());
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
const collect = require( 'collect.js' )
var myCollection = [ 'Monday' , 'Tuesday' , 'Thursday' ,
'Friday' , 'Saturday' , 'Sunday' ]
function make(items = []) {
return new this .constructor(items);
}
const collection = collect(myCollection);
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
Last Updated :
26 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...