type-safe-groupby
this is a typesafe way to use groupby in ts
Usage
import { groupBy } from "better-groupby";
const data = [
{ name: "a", age: 1 },
{ name: "b", age: 2 },
{ name: "a", age: 3 },
{ name: "b", age: 4 },
];
const grouped = groupBy(data, "name");
console.log(grouped);
const nestedDate = [
{ name: "a", age: 25, address: { city: "NY", country: "USA" } },
{ name: "b", age: 25, address: { city: "LA", country: "USA" } },
{ name: "a", age: 25, address: { city: "NY", country: "USA" } },
{ name: "b", age: 25, address: { city: "LA", country: "USA" } },
];
const nestedGrouped = groupBy(nestedDate, (item) => item.address.city);
console.log(nestedGrouped);
whats different from the original groupBy is that this one is typesafe, so you can use the keys of the object as a key to group by