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

194
Views
¿Cómo se vería una declaración de cambio que tiene que expresar una condición compleja que contiene ambos operadores lógicos (|| y &&)?

Tengo que construir una condición basada en uno o dos valores.

 const getLabel = (type?: string, typeXy?: string) => { let label; switch (type || typeXy) { case 'Value A': label = 'Label A'; break; case 'Value B': label = 'Label B'; break; case 'Value C': case 'Value X': label = 'Label C + X'; break; case 'Value Y': label = 'Label C + Y'; break; default: break; } return label; };

Necesito un estuche de dubbel en dos lugares. Cuando agrego case 'Value C': case 'Value Y': obtengo:

Caso duplicado label.eslintno-duplicate-case

1) Agrego // eslint-disable-next-line no-duplicate-case pero mi código aún no funciona. ¿O hay una forma más limpia de todos modos?

2) Puedo cambiar mi interruptor de or a and : (type || typeXy) pero, ¿entonces el primer caso no funciona? ¿Cómo apoyo tanto or and and ?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

¿Qué tal usar un objeto literal como tabla de búsqueda donde uno puede expresar con precisión, qué resultado para qué caso prefiere? ..

 const getLabel = (type/*?: string*/, typeXy/*?: string*/) => { return ({ 'Value A': 'Label A', 'Value B': 'Label B', 'Value C': 'Label C', 'Value X': 'Label X', 'Value Y': 'Label Y', 'Value CValue X': 'Label C + X', 'Value XValue C': 'Label C + X', 'Value CValue Y': 'Label C + Y', 'Value YValue C': 'Label C + Y', })[(type ?? '') + (typeXy ?? '')]; }; console.log("getLabel('Value A') ...", getLabel('Value A')); console.log("getLabel('Value B', null) ...", getLabel('Value B', null)); console.log("getLabel(null, 'Value C') ...", getLabel(null, 'Value C')); console.log("getLabel(undefined, 'Value X') ...", getLabel(undefined, 'Value X')); console.log("getLabel('Value Y') ...", getLabel('Value Y')); console.log("getLabel('Value C', 'Value X') ...", getLabel('Value C', 'Value X')); console.log("getLabel('Value X', 'Value C') ...", getLabel('Value X', 'Value C')); console.log("getLabel('Value C', 'Value Y') ...", getLabel('Value C', 'Value Y')); console.log("getLabel('Value Y', 'Value C') ...", getLabel('Value Y', 'Value C'));
 .as-console-wrapper { min-height: 100%!important; top: 0; }

over 4 years ago · Santiago Trujillo Report

0

Si entiendo su pregunta, desea verificar condicionalmente si se pasan ambos argumentos y, de ser así, use una comparación lógica AND; de lo contrario, si solo se pasa uno u otro, vuelva a una comparación lógica OR.

Use un interruptor externo para bifurcarse en la condición lógica AND/OR y luego use interruptores anidados para evaluar las combinaciones específicas AND u OR.

 const getLabel = (type /*?: string*/, typeXy /*?: string*/) => { switch (true) { case !!(type && typeXy): switch (type) { case "Value C": { let label = "Label C"; switch (typeXy) { case "Value X": return label + " + X"; case "Value Y": return label + " + Y"; } } } break; case !!(type || typeXy): switch (type || typeXy) { case "Value A": return "Label A"; case "Value B": return "Label B"; } } }; console.log(getLabel("Value A")); // Label A console.log(getLabel("Value B")); // Label B console.log(getLabel(undefined, "Value A")); // Label A console.log(getLabel(undefined, "Value B")); // Label B console.log(getLabel("Value C", "Value X")); // Label C + X console.log(getLabel("Value C", "Value Y")); // Label C + Y

over 4 years ago · Santiago Trujillo 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!