groupBy() in JavaScript2 Mar 2025 | 3 min read We will understand the groupBy() in JavaScript in this article. groupBy() methodThis method is utilized for grouping elements. It is a static method which permits to group of elements that are iterable. It is utilized to put elements in categories in different groups. Syntax:In the above-given syntax, groupBy is the function. The items and callbackFn are the arguments. The argument "items" is an iterable element that will be grouped such as an array. The argument "callbackFn" is the function utilized to execute for each element in the iterable. The value returned by the Object.groupBy() method is a null prototype object. The Object.groupBy() method calls the callbackFn function for every element present in the iterable. The callbackFn returns a string or symbol. A value returned as the string is utilized as the key for the object returned by Object.groupBy(). We will understand the groupBy() function with the help of demonstrations. Demonstration-1We will utilize the groupBy() method to group books on the basis of genre. Code: Output: We can witness four groups in the output with the names Fantasy, Fiction, Non-Fiction and Science Fiction. [Object: null prototype] { 'Fantasy': [ { title: 'The Lion, The Witch and The Wardrobe', genre: 'Fantasy' }, { title: 'Stardust', genre: 'Fantasy' } ], 'Fiction': [ { title: 'The Alchemist', genre: 'Fiction' }, { title: 'Pride and Prejudice', genre: 'Fiction' } ], 'Non-Fiction': [ { title: 'How Big Things Get Done', genre: 'Non-Fiction' } ], 'Science Fiction': [ { title: 'The Three-Body Problem', genre: 'Science Fiction' } ] } Demonstration-2We will utilize the groupBy() method to group cars by its company. Code: Output: We can witness three groups with the names Ferrari, Skoda and Honda. [Object: null prototype] { "Ferrari": [ { company: "Ferrari", model: "Roma", year: 2020 }, { company: "Ferrari", model: "812", year: 2023 } ], "Skoda": [ { company: "Skoda", model: "Slavia", year: 2023 } ], "Honda": [ { company: "Honda", model: "Elevate", year: 2023 }, { company: "Honda", model: "Amaze", year: 2022 } ] } Demonstration-3We will utilize the groupBy() method to group users by their year of subscription. Code: Output: We can witness the three groups namely 2021, 2023 and 2024. [Object: null prototype] { "2021": [ { userName: "Kavya", yearSubscription: 2021 }, { userName: "Ansh", yearSubscription: 2021 } ], "2023": [ { userName: "Karishma", yearSubscription: 2023 }, { userName: "Lavyansh", yearSubscription: 2023 } ], "2024": [ { userName: "Chandni", yearSubscription: 2024 }, { userName: "Harsh", yearSubscription: 2024 } ] } Demonstration-4We will utilize the groupBy() method to group numbers into an even group and an odd group. We will create a callback function for the n value and then check whether the number is even or odd. Code: Output: We can witness the result object with two groups with the names odd and even. [Object: null prototype] { odd: [ 1, 5, 3 ], even: [ 8, 2, 4 ] } Browser CompatibilityFollowing are the browsers that support the groupBy() method:
Conclusion:We have understood the groupBy() in JavaScript in this article. We have understood Object.groupBy() with the help of demonstrations. Next TopicJavascript-let-vs-var |
The current URL in the web is important for multiple use cases like analytics tracking, dynamic rendering or implementing conditional logic based on the page URL. JavaScript offers some methods and properties that help the developers to manipulate the URL of the current webpage. In this article,...
12 min read
In the ever-evolving landscape of web development, JavaScript libraries play a pivotal role in simplifying complex tasks, enhancing functionality, and speeding up development cycles. Our curated list features a wide array of libraries catering to various needs, from front-end frameworks for building dynamic user interfaces to...
19 min read
JavaScript has made some amazing progress since its beginning during the 1990s. From its unassuming starting points as a basic scripting language for adding intuitiveness to web pages, JavaScript has developed into a strong and flexible language that drives current web development. With the ascent...
12 min read
Data type conversion is a frequent job in JavaScript, particularly when working with user input or data from other sources. Converting values to numbers is one of the basic conversions. JavaScript offers several approaches and techniques to do this task effectively. We'll look at a...
5 min read
What is Debugging in JavaScript? In JavaScript, debugging is used to find errors or bugs in the source code of any software. In simple words, debugging is a process in which we examine the JavaScript code find the errors, and fix them. Developers can make some mistakes...
4 min read
Arrays are fundamental data structures in JavaScript, providing a flexible way to store multiple values in a single variable. One common question among JavaScript developers is whether arrays can be extended once they are created. Let's delve into this topic to understand the dynamics of extending...
4 min read
What is ? CoffeeScript is a little language that complies with JavaScript. CoffeeScript provides simple and easy-to-learn syntax, avoiding the complex syntax of JavaScript. In simple terms, it is a programming language that complies into JavaScript. It was designed to enhance JavaScript readability and reduce its verbosity by...
6 min read
A simple mechanism for manually triggering activities on DOM factors is dispatchEvent in JavaScript. It is a critical thing of the Document Object Model (DOM) API, which allows us programmers to simulate personal interactions to create dynamic, fairly interactive online programs. We need to observe a...
8 min read
The {X, Y} quantifier contains and matches numbers from the x to y range. The regex quantifier operates in a string of numbers with the regular expression's search, match, test, and exec method. We can use regex modifiers for the available character with the quantifier. Syntax To...
4 min read
Strings are important types of information used to display, operate, and handle operational data. A number is converted into a string using a JavaScript function. This article details multiple functions and methods for converting numbers into strings. Methods to convert into string in JavaScript The following ways are...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India