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

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

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