VOOZH about

URL: https://www.geeksforgeeks.org/javascript/collect-js-maptogroups-method/

⇱ Collect.js mapToGroups() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Collect.js mapToGroups() Method

Last Updated : 27 Nov, 2020

The mapToGroups() method is used to iterate through the collection elements and passes each value of collection into the given callback function.

Syntax:

collect(array).mapToGroups(callback)

Parameters: The collect() method takes one argument that is converted into the collection and then mapToGroups() method is applied on it. The mapToGroups() method holds the callback function as a parameter.

Return Value: This method returns the collection elements according to given callback.

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

Example 1:

Output:

{ Ashok: [ 75 ], Rakesh: [ 86, 98 ], Rajesh: [ 56 ] }

Example 2:

Output:

{
 '25-10-96': [ 'Rahul', 'Aditya', 'Rahul' ],
 '16-08-94': [ 'Abhishek' ]
}
Comment
Article Tags: