The whereNull() method in collect.js is used to filter the values from a collection that is null.
Installation:
- In NodeJs:
npm install collect.js
- CDN for collect.js
<script src="https://cdnjs.com/libraries/collect.js"></script>
Syntax:
whereNull(key if any);
Parameters: It accepts the key of the object.
Returns: It returns the collection object.
Example 1:
// 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);
Output:
Example 2: Using objects having null values.
// 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());