i made a type-safe GroupBy function.
/**
* Groups array of objects by a given key
* @param arr array of objects to group
* @param key must be present on every object, and it's values must be string|number
* @author telepresence
* @license CC-BY-4.0
*/
function groupBy(arr: T[], key: keyof T, defaultAcc: Record = {}) {
return arr.reduce((acc, val, i) => {
const compValue = val[key];
if (typeof compValue !== 'string' && typeof compValue !== 'number') {
throw new Error(`key ${key.toString()} has values other than string/number. can only group by string/number values`);
}
if (!acc[compValue]) acc[compValue] = []
acc[compValue].push(val);
return acc;
}, defaultAcc);
}
example:
const data = [{
"name": "jim",
"color": "blue",
"age": "22"
}, {
"name": "Sam",
"color": "blue",
"age": "33"
}, {
"name": "eddie",
"color": "green",
"age": "77"
}];
groupBy(data, 'color')
would result into:
{
"blue": [
{
"name": "jim",
"color": "blue",
"age": "22"
},
{
"name": "Sam",
"color": "blue",
"age": "33"
}
],
"green": [
{
"name": "eddie",
"color": "green",
"age": "77"
}
]
}
TL;DR i've sucessfully wrote something using generics in typescript for the first time, and i think it's pretty epic.
https://www.youtube.com/watch?v=-NNLraklIp0
Provided to YouTube by DistroKidEVERYTHING IS RED · Koven WeiEVERYTHING IS RED℗ LAB0916Released on: 2023-02-10Auto-generated by YouTube.
@telepresence
@discuss.tchncs.de