Collect.js unlessNotEmpty() Function

Collect.js is a fluent and convenient wrapper for working with arrays and objects. The unlessNotEmpty() function executes the callback function when the collection is empty.
Installation: Install the Collect.js module using the following command:
npm install --save collect.js
Syntax:
collection.unlessNotEmpty(callback)
Parameters: This function takes only one parameter i.e. the callback function which executes if the collection is empty.
Return Value: This function returns the new collection object.
Example 1: Filename-index.js
Javascript
// Requiring moduleconst collect = require('collect.js')// User defined collectionvar myCollection = []// Creating collection objectconst collection = collect(myCollection);// Callback execution since collection object is emptycollection.unlessNotEmpty(item => item.push(1));// Printing collectionconsole.log(collection.all()); |
Run the index.js file using the following command:
node index.js
Output:
[ 1 ]
Example 2: Filename-index.js
Javascript
// Requiring moduleconst collect = require('collect.js')// User defined collectionvar myCollection = [1, 2, 3]// Creating collection objectconst collection = collect(myCollection);// No callback execution since // collection is not emptycollection.unlessNotEmpty((item) => { item.push(4) item.push(5)});// Printing collectionconsole.log(collection.all()); |
Run the index.js file using the following command:
node index.js
Output:
[ 1, 2, 3]
Reference: https://collect.js.org/api/unlessNotEmpty.html
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek 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!



