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

91
Views
How to optimize multiple variable check
const {
    service,
    customer,
    company,
    parking,
    aircraftType,
    aircraft,
    endPlan,
    startPlan,
    heatingPointsMasterCodes,
    lavatoryType,
    passengersCategory } = formValues;

useEffect(() => {
    customer &&
        company &&
        (parking || service === ReferenceCodesOfServicesEnum.ProvisioningMinibus) &&
        aircraftType &&
        aircraft &&
        endPlan &&
        startPlan &&
        (heatingPointsMasterCodes ||
            lavatoryType ||
            passengersCategory ||
            formValues[DocumentItemNamesEnum.WaterSystemMaintenance] ||
            service === ReferenceCodesOfServicesEnum.AircraftCooling)
        ? setDisabled(false)
        : setDisabled(true);
}, [formValues]);

So my question is, how to optimize or reduce variable check for true value? First i get variables with destructing from object, then check same variables for true value. I think i can somehow optimize this, but dont know how

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

In general if you have a series of if checks you could consider turning it into a switch instead. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

Since you're mostly checking to make sure things are defined, but also have some more nuanced requirements, it may be better break it up slightly:

// Properties required irrespective of environment properties in DocumentItemNamesEnum or  ReferenceCodesOfServicesEnum
const requiredProperties = ["customer",
"company", "aircraftType",
"aircraft", "endPlan",
"startPlan"]

const requiredPropertiesDefined = requiredProperties.every(value => !!value)

useEffect(() => {
   if (requiredPropertiesDefined && 
       (formValues.parking || formValues.service === ReferenceCodesOfServicesEnum.ProvisioningMinibus) &&
   && (formValues.heatingPointsMasterCodes ||
  formValues.lavatoryType ||
  formValues.passengersCategory ||
  formValues[DocumentItemNamesEnum.WaterSystemMaintenance] ||
  service === ReferenceCodesOfServicesEnum.AircraftCooling) {
   setDisabled(true)
} else {
   setDisabled(false
   }
}, [formValues])
about 4 years ago · Juan Pablo Isaza Report

0

If you want to check if all values of object are truthy then following code can be used.

Object.values(formValues).every(value => !!value)
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!