Collect.js whereNotNull Method
Last Updated :
29 Jul, 2020
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
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
const collect = require( 'collect.js' );
let obj1 = [{ "a" : null }, { "a" : 3 },
{ "b" : null }, { "b" : 33 }, { "a" : null }];
let collection = collect(obj1);
let collectionFilter = collection.whereNotNull( "a" );
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
const collect = require( 'collect.js' );
let obj1 = [1, 2, null , null , 5];
let collection = collect(obj1);
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...