Collect.js tap() Function
Collect.js is a fluent and convenient wrapper for working with arrays and objects. The tap() function accepts the collection as a parameter and without affecting the collection it allows the user to tap into the collection at a specific point and do anything with the item.
Installation: Install the Collect.js module using the following command:
npm install --save collect.js
Syntax:
collection.tap(callback)
Parameters: This function takes only one parameter i.e. the callback function which executes as per user need.
Return Value: This function returns the new collection object.
Example 1: Filename-index.js
Javascript
const collect = require( 'collect.js' )
var myCollection = [2, 4, 3, 1, 5]
var newCollection = collect(myCollection)
.tap((collection) => {
console.log(collection.all());
})
console.log(newCollection)
|
Run the index.js file using the following command:
node index.js
Output:
[ 2, 4, 3, 1, 5 ]
Collection { items: [ 2, 4, 3, 1, 5 ] }
Example 2: Filename-index.js
Javascript
const collect = require( 'collect.js' )
var myCollection = [-1,2,0,25]
collect(myCollection).sort().tap((collection) => {
console.log(collection.all());
})
|
Run the index.js file using the following command:
node index.js
Output:
[ -1, 0, 2, 25 ]
Reference: https://collect.js.org/api/tap.html
Last Updated :
12 Jan, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...