Collect.js whereNotNull 方法
whereNotNull()方法用于返回不包含任何空值的集合。简单来说,就是过滤不为空的值。
安装:
- 在 NodeJs 中:
npm install collect.js
- collect.js 的 CDN
句法:
whereNotNull(key if any)
参数:它获取对象中元素的键。
返回值:返回对象。
示例 1:
Javascript
// Importing the collect.js module.
const collect = require('collect.js');
let obj1 = [{ "a": null }, { "a": 3 },
{ "b": null }, { "b": 33 }, { "a": null }];
// Making a collection
let collection = collect(obj1);
// Filtering the null values;
let collectionFilter = collection.whereNotNull("a");
// Printing the original collection
console.log("Original collection is: ", collection.all())
console.log("filter only key \"a\" for null values")
console.log("Filtered collection is: ",
collectionFilter.all());
Javascript
// Importing the collect.js module.
const collect = require('collect.js');
let obj1 = [1, 2, null, null, 5];
// Making a collection
let collection = collect(obj1);
// Filtering the null values;
let collectionFilter = collection.whereNotNull();
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 = [1, 2, null, null, 5];
// Making a collection
let collection = collect(obj1);
// Filtering the null values;
let collectionFilter = collection.whereNotNull();
console.log("Original collection is: ",
collection.all())
console.log("Filtered collection is: ",
collectionFilter.all());
输出:
参考: https://collect.js.org/api/whereNotNull.html