![]() |
VOOZH | about |
Lodash's _.groupBy() method groups elements of a collection into an object, using the results of an iterated function as keys, with corresponding arrays of elements that generate each key.
Syntax:
_.groupBy(collection, [iteratee]);Parameters:
Return Value: This method returns the composed aggregate object.
Example 1: In this example, we are composing an array-by-length function where the array is grouped by the length of the strings.
Output:
{ '4': [ 'nine', 'four' ], '5': [ 'eight', 'seven' ]}Example 2: In this example, we are composing an array by in which array is grouped according to the Math.floor function.
Output:
{ '3': [ 'one', 'two' ], '4': [ 'four' ], '5': [ 'three' ] }
{ '1': [ 1.2 ], '3': [ 3.1, 3.3 ] }