Collect.js是用於處理數組和對象的流利且便捷的包裝器。這個unlessNotEmpty()函數當集合為空時執行回調函數。
安裝:使用以下命令安裝Collect.js模塊:
npm install --save collect.js
用法:
collection.unlessNotEmpty(callback)
參數:此函數僅接受一個參數,即,如果集合為空,則執行該回調函數。
返回值:此函數返回新的集合對象。
範例1:Filename-index.js
Javascript
// Requiring module
const collect = require('collect.js')
// User defined collection
var myCollection = []
// Creating collection object
const collection = collect(myCollection);
// Callback exection since collection object is empty
collection.unlessNotEmpty(item => item.push(1));
// Printing collection
console.log(collection.all());
使用以下命令運行index.js文件:
node index.js
輸出:
[ 1 ]
示例2:Filename-index.js
Javascript
// Requiring module
const collect = require('collect.js')
// User defined collection
var myCollection = [1, 2, 3]
// Creating collection object
const collection = collect(myCollection);
// No callback execution since
// collection is not empty
collection.unlessNotEmpty((item) => {
item.push(4)
item.push(5)
});
// Printing collection
console.log(collection.all());
使用以下命令運行index.js文件:
node index.js
輸出:
[ 1, 2, 3]
相關用法
- 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 unlessNotEmpty() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。