Tuesday, December 31, 2024
Google search engine
HomeLanguagesJavascriptCollect.js whereNotNull Method

Collect.js whereNotNull Method

The whereNotNull() method is used to return a collection that does not contain any null values. In simple words, it filters the values that are not null.

Installation:

  • In NodeJs:
    npm install collect.js
  • CDN for collect.js
    <script src="https://cdnjs.com/libraries/collect.js"></script>

Syntax:

whereNotNull(key if any)

Parameters: It takes the key of the element in an object.

Return Value: It returns the object.

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


Output:

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


Output:

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments