Syntax, Parameter and Return Value
Syntax:
_.groupBy(collection, [iteratee=_.identity])
Parameters:
-
collection (Array|Object): The collection to iterate over.
- [iteratee=_.identity] (Function): The iteratee to transform keys.
Return Value:
(Object) - Returns the composed aggregate object.
Examples
Grouping Numbers by Even and Odd:
JavaScript
var _ = require('lodash');
var numbers = [1, 2, 3, 4, 5, 6];
var groupedByEvenOdd = _.groupBy(numbers, num => num % 2 === 0 ? 'even' : 'odd');
console.log(groupedByEvenOdd);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'odd': [1, 3, 5], 'even': [2, 4, 6] }
Demonstrates grouping numbers based on their parity.
Categorizing Strings by Length:
JavaScript
var words = ['one', 'two', 'three', 'four', 'five'];
var groupedByLength = _.groupBy(words, 'length');
console.log(groupedByLength);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ '3': ['one', 'two'], '4': ['four', 'five'], '5': ['three'] }
Shows how to group strings based on their length.
Grouping Objects by a Property:
JavaScript
var people = [{ 'name': 'Alice', 'age': 21 },
{ 'name': 'Bob', 'age': 21 },
{ 'name': 'Cindy', 'age': 25 }];
var groupedByAge = _.groupBy(people, 'age');
console.log(groupedByAge);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ '21': [{ 'name': 'Alice', 'age': 21 }, { 'name': 'Bob', 'age': 21 }], '25': [{ 'name': 'Cindy', 'age': 25 }] }
An example of grouping objects based on a shared property.
Advanced Grouping with a Composite Key:
JavaScript
var items = [{ 'type': 'fruit', 'name': 'apple' },
{ 'type': 'vegetable', 'name': 'carrot' },
{ 'type': 'fruit', 'name': 'banana' }];
var groupedByTypeAndName = _.groupBy(items, item => `${item.type}-${item.name}`);
console.log(groupedByTypeAndName);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'fruit-apple': [{...}], 'vegetable-carrot': [{...}], 'fruit-banana': [{...}] }
Demonstrates grouping based on a composite key created from multiple object properties.
Frequently Asked Questions
Can _.groupBy() handle complex grouping criteria?
Yes, _.groupBy() can handle complex criteria by providing a custom iteratee function that returns the desired key based on which to group.
What happens to elements that don't match any group?
Elements that don't match any group are categorized under a key determined by the return value of the iteratee function. If the iteratee returns undefined, they are grouped under the undefined key.
Is _.groupBy() efficient for large datasets?
_.groupBy() is generally efficient, but performance may vary with the complexity of the iteratee function and the size of the dataset.
Conclusion
Lodash's _.groupBy() method is a powerful and flexible tool for organizing elements in a collection into groups based on specified criteria. It simplifies data categorization, aiding in efficient data analysis and manipulation across a wide range of applications.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.