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

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

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 Denunciar

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