Javascript new Array Grouping Methods | Array Grouping Methods

I’m an open source enthusiast and I am passionate Web Development, DevOps & I enjoy learning new things.
What is this?
The good news is that JavaScript is now getting grouping methods so you won’t have to anymore. Object.groupBy and Map.groupBy are new methods that will make grouping easier and save us time or dependency.
How is it implemented?
We have given both examples but try them when they come. You can track this- feature
Implement Object.groupBy
Object.groupBy returns a null-prototype object. This means the object does not inherit any properties from Object.prototype. This is great because it means you won’t accidentally overwrite any properties on Object.prototype, but it also means that the object doesn’t have any of the methods you might expect, like hasOwnProperty or toString.
const people = [
{ name: "Alice", age: 28 },
{ name: "Bob", age: 30 },
{ name: "Eve", age: 28 },
];
const peopleByAge = Object.groupBy(people, (person) => person.age);
console.log(peopleByAge[28]);
// => [{"name":"Alice","age":28}, {"name":"Eve","age":28}]
console.log(peopleByAge["28"]);
// => [{"name":"Alice","age":28}, {"name":"Eve","age":28}]
Implement Map.groupBy
Map.groupBy does almost the same thing as Object.groupBy except it returns a Map. This means that you can use all the usual Map functions. It also means that you can return any type of value from the callback function.
const ceo = { name: "Jamie", age: 40, reportsTo: null };
const manager = { name: "Alice", age: 28, reportsTo: ceo };
const people = [
ceo
manager,
{ name: "Bob", age: 30, reportsTo: manager },
{ name: "Eve", age: 28, reportsTo: ceo },
];
peopleByManager.get(ceo);
// => [{ name: "Alice", age: 28, reportsTo: ceo }, { name: "Eve", age: 28, reportsTo: ceo }]
peopleByManager.get({ name: "Jamie", age: 40, reportsTo: null });
// => undefined




