Open In App

Collect.js whereNull() Method

Last Updated : 29 Jul, 2020
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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:

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);

Output:

Example 2: Using objects having null values.

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());

Output:

Reference: https://collect.js.org/api/whereNull.html

Similar Reads