如果給定的第一個參數為true,則when()函數用於回調。在JavaScript中,首先將數組轉換為一個集合,然後將該函數應用於該集合。語法:
data.when(conditional ,rule)
Parameters: 該函數接受上述和以下所述的兩個參數:
- rule: 此參數保存要應用於集合的操作規則或條件。
- conditional: 此參數保留條件值true或false。
返回值:返回修改後的收藏列表。
下麵的示例說明了collect.js中的when()函數:示例1:在此示例中,我們獲取一個集合,然後使用when()函數修改該集合。
Javascript
// It is used to import collect.js library
const collect = require('collect.js');
const collection = collect([0 , 1 , 2]);
collection.when(true, items => items.push(3));
console.log(collection.all());
輸出:
[0 , 1 , 2 , 3]
範例2:與上麵的示例相同,但是應用了不同的操作來執行。
Javascript
// It is used to import collect.js library
const collect = require('collect.js');
const collection = collect([0 , 1 , 2]);
collection.when(true, items => items.put('Jason'));
console.log(collection.all());
輸出:
[ 0, 1, 2, Jason:undefined ]
相關用法
- PHP imagecreatetruecolor()用法及代碼示例
- p5.js year()用法及代碼示例
- d3.js d3.utcTuesdays()用法及代碼示例
- PHP ImagickDraw getTextAlignment()用法及代碼示例
- PHP Ds\Sequence last()用法及代碼示例
- 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()用法及代碼示例
- p5.js removeElements()用法及代碼示例
- PHP Imagick adaptiveSharpenImage()用法及代碼示例
- PHP XMLWriter endDtdEntity()用法及代碼示例
注:本文由純淨天空篩選整理自akhilsharma870大神的英文原創作品 Collect.js | when() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。