Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Collect.js eachSpread() Method

  • Last Updated : 25 Nov, 2020

The eachSpread() method is used to iterate over the collection items and pass each nested item value of collection into the given callback function.

Syntax:

collection.eachSpread()

Parameters: The collect() method takes one argument that is converted into the collection and then eachSpread() method is applied on it, which can take element if you apply it on the collection of objects.

Return Value: This method iterates over the collection of items.

Below example illustrate the eachSpread() method in collect.js:

Example 1:

Javascript




const collect = require('collect.js');
  
const collection = collect([
    ['Rakesh', 80],
    ['Shyam', 94],
    ['Sandeep', 75],
    ['Ashok', 88]
]);
  
collection.eachSpread((name, marks) => {
    console.log("Name => " + name 
        + " | Marks => " + marks)
});

Output:

Name => Rakesh | Marks => 80
Name => Shyam | Marks => 94
Name => Sandeep | Marks => 75
Name => Ashok | Marks => 88

Example 2:

Javascript




const collect = require('collect.js');
  
let arr = [
    ['Rahul', 98],
    ['Aditya', 96],
    ['Abhishek', 80],
];
  
// Converting object to collection 
const collection = collect(arr);
  
collection.eachSpread((name, score) => {
    console.log(name, score);
});
  
console.log(collection.eachSpread(
    (name, score) => false)
);

Output:

Rahul 98
Aditya 96
Abhishek 80
Collection {
 items: [ 
   [ 'Rahul', 98 ], 
   [ 'Aditya', 96 ], 
   [ 'Abhishek', 80 ] 
 ]
}

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 :

Start Your Coding Journey Now!