Skip to content
Related Articles

Related Articles

Improve Article

Collect.js | count() Function

  • Last Updated : 05 Jun, 2020
Geek Week

The count() function is used to count the number of collections in the element. In JavaScript, the array is first converted to a collection and then the function is applied to the collection.

Syntax: 

data.count()

Parameters: This function does not accept any parameter
Return Value:  Returns the count of the element in that collection.
 

Below examples illustrate the count() function in collect.js
Example 1: 

Javascript






// It is used to import collect.js library
const collect = require('collect.js');
  
const num = [0 , 1 , 2 , 3 , 4, 5 , 6, 7, 8, 9]; 
const data = collect(num);
const total = data.count();
  
console.log('Total number of elements are:', {total});

Output:

Total number of elements are: { total: 10 }

Example 2:

Javascript




// It is used to import collect.js library
const collect = require('collect.js');
  
const collection = collect([1, 2, 3, 4]);
const x = collection.count();
  
console.log(`Total number of elements are : ${x}`);

Output:

Total number of elements are : 4

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

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!

My Personal Notes arrow_drop_up
Recommended Articles
Page :