Collect.js是用於處理數組和對象的流利且便捷的包裝器。這個unlessEmpty()函數當集合不為空時執行回調函數。
安裝:使用以下命令安裝Collect.js模塊:
npm install --save collect.js
用法:
collection.unlessEmpty(callback)
參數:此函數僅接受一個參數,即如果集合不為空則執行的回調函數。
返回值:此函數返回新的集合對象。
範例1:Filename-index.js
Javascript
// Requiring module
const collect = require('collect.js')
// User defined collection
var myCollection = [1, 2]
// Creating collection object
const collection = collect(myCollection);
// Callback execution since collection
// object is not empty
collection.unlessEmpty(item => item.push(3));
// Printing collection
console.log(collection.all());
使用以下命令運行index.js文件:
node index.js
輸出:
[ 1, 2, 3 ]
示例2:Filename-index.js
Javascript
// Requiring module
const collect = require('collect.js')
// User defined collection
var myCollection = []
// Creating collection object
const collection = collect(myCollection);
// No callback execution since
// collection is not empty
collection.unlessEmpty((item) => {
item.push(1)
item.push(2)
});
// Printing collection
console.log(collection.all());
使用以下命令運行index.js文件:
node index.js
輸出:
[ ]
相關用法
- PHP imagecreatetruecolor()用法及代碼示例
- p5.js year()用法及代碼示例
- d3.js d3.utcTuesdays()用法及代碼示例
- PHP ImagickDraw getTextAlignment()用法及代碼示例
- PHP Ds\Sequence last()用法及代碼示例
- PHP Imagick floodFillPaintImage()用法及代碼示例
- PHP array_udiff_uassoc()用法及代碼示例
- PHP geoip_continent_code_by_name()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP GmagickPixel setcolor()用法及代碼示例
- PHP opendir()用法及代碼示例
- PHP cal_to_jd()用法及代碼示例
- d3.js d3.bisectLeft()用法及代碼示例
- PHP stream_get_transports()用法及代碼示例
- PHP Ds\Deque pop()用法及代碼示例
- PHP SimpleXMLElement children()用法及代碼示例
注:本文由純淨天空篩選整理自gouravhammad大神的英文原創作品 Collect.js unlessEmpty() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。