當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Collect.js pipe()用法及代碼示例

pipe()方法用於保留回調函數並將此函數應用於集合中並返回相應的結果。

用法:

collect(array).pipe(callback)

參數:collect()方法采用一個參數,該參數將轉換為集合,然後將pipe()方法應用於該參數。 pipe()方法將回調保留為參數。

返回值:此方法返回應用於集合的回調函數的結果。

模塊安裝:使用以下命令從項目的根目錄安裝collect.js模塊:



npm install collect.js

以下示例說明了collect.js中的pipe()方法:

示例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);

使用以下命令運行index.js文件:

node index.js

輸出:

22

示例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);

使用以下命令運行index.js文件:

node index.js

輸出:

84

相關用法


注:本文由純淨天空篩選整理自AshokJaiswal大神的英文原創作品 Collect.js pipe() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。