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

175
Visualizações
How to change the function call based on value

There are 3 functions imported:

import { searchShip, searchLcl, searchFcl } from "services/logisticsService";

searchData = {
  currentPort: currentPort,
  destinationPort: destinationPort,
  date: readyTOLoad,
  type: containerType,  // containerType is geting 1 value among this (ship,lcl,fcl)
}

Here I want to call one of those function based on that containerType value. For example:

If I get type value as ship then it should be

searchShip(searchData).then((response) => {
  console.log(response)
})

If I get type value as lcl then it should be

searchLcl(searchData).then((response) => {
  console.log(response)
})

If I get type value as fcl then it should be

searchFcl(searchData).then((response) => {
  console.log(response)
})

Is there any good way to call that search function on that type condition without if else statement because inside it there is very lengthy process to handle response.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Yes, you can define an object that has a {[key]: function} structure.

const handlersByType = {
  ship: searchShip,
  lcl: searchLcl,
  fcl: searchFcl.
};

handlersByType[type](searchData).then((response) => {
  console.log(response)
})
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