Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
Is there an alternative to lodash .chain function?

How do I achieve this without lodash chain function?

const results = chain(sortedItems || allItems)
            .filter((x) => this.filterItemsBySearchParam<T>(x, search))
            .filter((x) => this.filterItemsByFacets<T>(x, facets))
            .groupBy((x) => (groupBy ? [groupBy.split(',').map((path) => get(x, path))] : ''))
            .map((filteredItems: any, key) => {
                if (!isNaN(Number(limit))) {
                    filteredItems = [...filteredItems.slice(0, limit)];
                }

                return this.addData<T>(key, filteredItems.length, filteredItems);
            })
            .value();

I have tried using lodash flow, and some other ES6 functions, but none of them worked as expected. it could be that I'm not applying them correctly?

I have tried this:

const result = sortedItems || allItems
        .filter((x) => this.filterItemsBySearchParam<T>(x, search))
        .filter((x) => this.filterItemsByFacets<T>(x, facets))
        .groupBy((x) => (groupBy ? [groupBy.split(',').map((path) => get(x, path))] : ''))
        .map((filteredItems: any, key) => {
            if (!isNaN(Number(limit))) {
                filteredItems = [...filteredItems.slice(0, limit)];
            }

            return this.addData<T>(key, filteredItems.length, filteredItems);
        });

But the groupBy keeps throwing an error: groupBy is not a type of T.

I tried flow from here: Better and more performant than lodash chain, but like I said, I could not get it to work. Is there a way to achieve this? By the way, the .filter, .map, and .groupBy are all built-in ts functions.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try this:

const filteredItems = (sortedItems || allItems || []).filter(x => this.filterItemsBySearchParam<T>(x, search) && this.filterItemsByFacets<T>(x, facets));
const results = Object.entries(groupBy ? filteredItems.reduce((map, x) => {
    const key = JSON.stringify((groupBy.match(/[^,]+/g) || []).map(path => get(x, path)));
    (map[key] || (map[key] = [])).push(x);
    return map;
}, {}) : {'': filteredItems}).map(([key, filteredItems]) => {
    if (+limit > 0) {
        filteredItems = filteredItems.slice(0, limit);
    }

    return this.addData<T>(key, filteredItems.length, filteredItems);
});

ask if you have any questions :)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda