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

139
Views
Alternative to switch, loop in javascript for multiple conditions

I am implementing a rest API that incorporates pagination and sorting.

my query params:

  • for paging - page (mandatory), limit ( optional).
  • for sorting - order_by(mandatory), sort_by(optional)

For a particular get request, a user can do the combination of the following operations:-

  • use paging
  • use paging + sorting
  • use sorting
  • use paging (without limit) + sorting( without sort_by)
  • use paging (with limit) + sorting( with sort_by)

I was thinking to implement switch for this, by if in future additional query params are added then the list can go long

so is there an alternative where I can mention multiple conditions ( in OR form) and doesn't loose performance too?

Based on comments :- the same thing can be achieved by 5 if statements but objective is to have scalable, readable and little performance ( if its possible)

function getResults(object) {
  var title = object.params
  switch (title) {
    case 'page':
      title = 'page'
      break
    case 'order_By':
      title = 'orderBy'
    default:
      title = 'Unassigned'
      break
  }
  .......( continued)
  return title
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Just create an object a bit like this one

   Page
   PageSize (Limit i guess? Since paging in itself has no limit, just the pages)
   Sort
   SortDirection

I think this should suit you?

Then apply conditions via method. Example:

var myPagingObject = {};
function applyPagingParameter(key, value) {
 myPagingObject[key] = value;
}

No if statements need like that (except in the backend ofcourse, but if you standardize 4 if statements that can be used applicationwide, why not?)

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!