Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!