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
// Requiring the module const collect = require( 'collect.js' ); // Creating collection object const collection = collect([1, 2, 3, 5, 6, 8, 9, 11, 17, 22]); // Function call const pipe_val = collection.pipe( element => element.max()); // Printing the values console.log(pipe_val); |
Run the index.js file using the following command:
node index.js
Output:
22
Example 2: Filename: index.js
Javascript
// Requiring the module const collect = require( 'collect.js' ); // Creating collection object const collection = collect([1, 2, 3, 5, 6, 8, 9, 11, 17, 22]); // Function call const pipe_sum = collection.pipe( element => element.sum()); // Printing the values console.log(pipe_sum); |
Run the index.js file using the following command:
node index.js
Output:
84