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

174
Views
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 answers
Answer question

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