Tuesday, December 31, 2024
Google search engine
HomeLanguagesJavascriptCollect.js pipe() Method

Collect.js pipe() Method

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