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

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 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