Executando verificação de segurança...
3

Gosto muito de criar índices para essa solução, tenho uma função bem simples que me ajuda com isso:

function createIndex<T>(list: T[], key: keyof T) {
    const DEFAULT_INDEX = {} as Record<string, T[]>;

    return list.reduce((index, item) => {
        const indexKey = String(item[key] as keyof typeof index); 
        const existKey = Boolean(index[indexKey]);
        
        if (!existKey) {
            index[indexKey] = [];
        }

        index[indexKey].push(item);

        return index;
    }, DEFAULT_INDEX);
}
1

Nossa ficou muito bom, eu já precisei trabalhar com esse tipo de operação e acabei fazendo nas piores formas possíveis kkkkkk

Segue uma outra alternativa utilizando a estrutura Map:

function createIndex<T>(list: T[], key: keyof T) {
    const index = new Map<string, T[]>();

    for (const item of list) {
        const indexKey = String(item[key]);
        const itemsForKey = index.get(indexKey) || [];
        itemsForKey.push(item);
        index.set(indexKey, itemsForKey);
    }

    return index;
}
1