Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda