Collect.js whereNull() 方法
collect.js 中的whereNull()方法用于过滤集合中的值为 null 的值。
安装:
- 在 NodeJs 中:
npm install collect.js
- collect.js 的 CDN
句法:
whereNull(key if any);
参数:它接受对象的键。
返回:它返回集合对象。
示例 1:
Javascript
// Importing the collect.js module.
const collect = require('collect.js');
let obj1 = [1, null, 3, null, 4, 5];
// Making a collection
let collection = collect(obj1);
// Filtering the null values;
let collectionFilter = collection.whereNull();
console.log("Original collection is: ", collection)
console.log("Filtered collection is: ", collectionFilter);
Javascript
// Importing the collect.js module.
const collect = require('collect.js');
let obj1 = [{ "a": null }, { "a": 1 },
{ "b": null }, { "a": 3 }, { "a": null }];
// Making a collection
let collection = collect(obj1);
// Filtering the null values;
let collectionFilter = collection.whereNull("a");
// Printing the original collection
console.log("Original collection is: ",
collection.all())
console.log("Filtered collection is: ",
collectionFilter.all());
输出:
示例 2:使用具有空值的对象。
Javascript
// Importing the collect.js module.
const collect = require('collect.js');
let obj1 = [{ "a": null }, { "a": 1 },
{ "b": null }, { "a": 3 }, { "a": null }];
// Making a collection
let collection = collect(obj1);
// Filtering the null values;
let collectionFilter = collection.whereNull("a");
// Printing the original collection
console.log("Original collection is: ",
collection.all())
console.log("Filtered collection is: ",
collectionFilter.all());
输出:
参考: https://collect.js.org/api/whereNull.html