Collect.js pipe() Method
Last Updated :
30 Nov, 2020
The pipe() method is used to hold a callback function and apply this function into the collection and returns the corresponding result.
Syntax:
collect(array).pipe(callback)
Parameters: The collect() method takes one argument that is converted into the collection and then the pipe() method is applied to it. The pipe() method holds the callback as a parameter.
Return Value: This method returns the result of the callback function that applied to the collection.
Module Installation: Install collect.js module using the following command from the root directory of your project:
npm install collect.js
The below example illustrates the pipe() method in collect.js:
Example 1: Filename: index.js
Javascript
const collect = require( 'collect.js' );
const collection = collect([1,
2, 3, 5, 6, 8, 9, 11, 17, 22]);
const pipe_val = collection.pipe(
element => element.max());
console.log(pipe_val);
|
Run the index.js file using the following command:
node index.js
Output:
22
Example 2: Filename: index.js
Javascript
const collect = require( 'collect.js' );
const collection = collect([1, 2,
3, 5, 6, 8, 9, 11, 17, 22]);
const pipe_sum = collection.pipe(
element => element.sum());
console.log(pipe_sum);
|
Run the index.js file using the following command:
node index.js
Output:
84
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...