Wednesday, October 22, 2025
HomeLanguagesJavascriptCollect.js each() Method

Collect.js each() Method

The each() function iterates over the items in the collection and passes each item to a callback. In JavaScript, the array is first converted to a collection and then the function is applied to the collection.

Syntax: 
 

data.each(item)

Parameters: This function accept a single parameter as mentioned above and described below:
 

  • item: This parameter holds the collection item where any operation will be performed on the collection on items.

Return Type: Return a result after performing the define operator operation.

Below examples illustrate the each() function in collect.js
Example 1: Here in this example, we take a collection and then using the each() method , we apply + operation to the collection.

Javascript




// It is used to import collect.js library
const collect = require('collect.js');
  
const num = [0 , 1 , 2 , 3 , 4];
let x = 0;
const data = collect(num);
  
data.each((item) => {
    x += item;
});
  
console.log(`sum = ${x}`);


Output: 

sum = 10

Example 2:

Javascript




// It is used to import collect.js library
const collect = require('collect.js');
  
  
let sum = 0;
const collection = collect([1 , 5 , 7 , 9]);
  
collection.each((item) => {
  sum += item;
});
  
console.log(sum);


Output: 

22

Reference: https://collect.js.org/api/each.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

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS