Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda