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

104
Vistas
How to collate the two javascript arrays produced by the vuetify options.sortBy and sortDesc for pushing to mongodb aggregate pipeline?

Given the two javascript arrays produced by the vuetify table options.sortBy and sortDesc:

options.sortBy = ['name', 'email'];  // fields to sort
options.sortDesc = [ false, true ];  // whether to sort field descending

Desired result:

sort = { $sort: { name: -1, email: 1 } };

So that we can push into mongodb aggregate pipeline array like:

pipeline.push(sort);

Code available in this branch.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It ends up being a good case for reduce. Here's what I ended up with:

type SortValue = 'asc' | 'desc' | 'ascending' | 'descending' | 1 | -1;

let sort:{[key:string]:SortValue} =
  query.sortBy === undefined ||
  query.sortDesc === undefined ? undefined :
  query.sortBy.split(',').reduce((sort:{[key:string]:SortValue}, field:SortValue, index:number) => {
    const sortDesc = query.sortDesc.split(',')[index];
    const sortValue = !!(parseInt(sortDesc) || sortDesc === "true") ? 1 : -1;
    sort[field] = sortValue;
    return sort;
  }, {});

pagination.sort = sort;

Open to improvements/variants. Thanks!

Code available in this branch.

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