Skip to main content

Command Palette

Search for a command to run...

Exploring the Power of Object GroupBy in JavaScript

Published
3 min read
Exploring the Power of Object GroupBy in JavaScript
S

I am a developer from Nairobi Kenya

Introduction

Developers working with JavaScript frequently deal with complex datasets and object collections. Managing and organizing this data efficiently is essential for writing clean and maintainable code. One powerful tool that comes to the rescue is the groupBy function, which enables developers to group objects based on a specific criterion or property. In this article, we will dive deep into the concept of Object groupBy, and understand how it can simplify data manipulation tasks.

Understanding Object GroupBy

The groupBy function is not native to JavaScript, but it is commonly implemented in various libraries such as Lodash or implemented manually in custom code. At its core, the groupBy function takes a collection of objects and groups them based on a specified key or property. This results in a new object where each unique key corresponds to an array of objects that share the same value for the specified property.

Syntax

The basic syntax of a groupBy function can be represented as follows:

Object.groupBy(items, callbackFn)

items is an iterable (such as an array) whose elements will be grouped.

callbackFn is a function to execute for each element in the iterable.

Implementation

Imagine you have an array of objects, where the objects share a similar schema, these might be records from a database where every object has a field of age.

You can use groupBy to separate the children from the adults by defining a function that groups anyone under 21 into children and anyone over that into adults.

// Import the 'core-js' library
import "core-js";
// An array of student objects with 'name' and 'age' properties.
let students = [
  { name: "john", age: 2 },
  { name: "sheldon", age: 19 },
  { name: "Jeff", age: 69 },
  { name: "Tristan", age: 35 },
  { name: "Napoleon", age: 45 },
  { name: "Alexander", age: 20 },
];

// Function to determine whether a person is a child or an adult based on age.
function adultsOnly({ age }) {
  // If the age is less than or equal to 21, consider them as "children".
  if (age <= 21) {
    return "children";
  } else {
    // Otherwise, consider them as "adults".
    return "adults";
  }
}
// Use the 'groupBy' method on the Object to organize students into groups based on the 'adultsOnly' function.
const organized = Object.groupBy(students, adultsOnly);

// Display the result, which is an object with properties representing groups ("children" or "adults") and values as arrays containing corresponding student objects.
console.log(organized);

The output of the above code is as follows.

[Object: null prototype] {
  children: [
    { name: 'john', age: 2 },
    { name: 'jane', age: 19 },
    { name: 'Alexander', age: 20 }
  ],
  adults: [
    { name: 'Jeff', age: 69 },
    { name: 'Tristan', age: 35 },
    { name: 'Napoleon', age: 45 }
  ]
}

The return value is a null-prototype object with properties for all groups, each assigned to an array containing the elements of the associated group.

Browser Compatibility

When dealing with JavaScript features or functions, it's crucial to consider browser compatibility. Since groupBy is not native to JavaScript, it is currently supported in the following browsers.

  • Chrome

  • Edge

  • Firefox

  • Opera

  • Safari (Preview browser support)

object groupBy function is also supported in the following runtimes.

  • Node.js

  • Deno

Conclusion

To sum up, the Object groupBy function is a powerful tool in the JavaScript developer's arsenal, offering a convenient way to organize and manipulate complex datasets. By understanding and leveraging this function effectively, developers can simplify their code, improve data management, and ultimately enhance the overall efficiency of their JavaScript applications.