The _.groupBy() function is used to make collection of the elements in the array passed. It works by matching the value of each element to the other. If they matches then they are put in one collection otherwise we will have 2 collections/groups. We can also pass a function based on who’s result we will collect the elements. It can group both on the basis of number and also by string.
_.groupBy( list, iteratee, context )
Parameters: This function accepts three parameters as mentioned above and described below:
- List: This parameter contains the element list.
- Iteratee: This parameter contains the condition which is used to text the elements.
- Context: It is the text which is used to display. It is optional parameter.
Return values: It returns the collections as the different arrays.
Using Math.ceil() in the _.groupBy() function: The _.groupBy() function takes the element from the list one by one and pass it to the Math.ceil() function. Then each elements output of the function will be matched with the output of another element in the Math.ceil() then they are put in 1 group otherwise they will be in 2 separate groups. After all the elements are matched with all the rest elements then the _.groupBy function ends.
Using length() in the _.groupBy() function: Passing the array elements to the groupBy() function and match the elements on the basis of their length. If the length of two elements is same then the they will be grouped in 1 group otherwise 2 groups will be formed.
Using a property of the array passed in the _.groupBy() function: First, declare the array (here array is ‘arr’). Choose one condition on which need to check like here ‘prop3’. Then the elements which have the same value in the ‘prop3’ will be grouped in 1 group. Console.log the final answer.
Passing ‘date’ as property of the array to the _.groupBy() function together: First define an array with one property as ‘date’ of the format ‘dd-mm-yy’. Then pass the array and the ‘date’ property to the _.groupBy() function. The elements having the same date will be grouped in 1 group. The group numbering will start from 0.
- Lodash _.groupBy() Method
- jQuery | prepend() with Examples
- jQuery | dblclick() with Examples
- jQuery | offset() with Examples
- jQuery | children() with Examples
- jQuery | Hide/Show, Toggle and Fading methods with Examples
- jQuery | focusin() with Examples
- jQuery | Animation, Slide methods with Examples
- jQuery | change() with Examples
- jQuery | filter() with Examples
- jQuery | height() and innerHeight() with Examples
- jQuery | eq() with Examples
- jQuery | empty() with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh