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

168
Views
¿Podría simplificar el siguiente código OR lógico de javascript?

Tengo un código en mi proyecto, pero es un poco torpe. ¿Alguien podría ayudarme a simplificar el siguiente código?

if (a == "good" || a == "beautiful || a == "pretty"|| a == "excellent" || a == "superb"|| a == "spectacular")

¿Podría convertir esto en una especie de matriz y luego usar esa matriz en este código if?

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

0

Supongo que podría usar una estructura Hash-Set en su lugar: en JS esto es Set (o Set<T> en TypeScript):

 const positiveWords = new Set( [ "good", "beautiful", "pretty", "excellent", "superb", "spectacular" ] );

Uso:

 if( positiveWords.has( a ) ) { }

Tenga en cuenta que primero deberá convertir a a minúsculas. Si desea que no se distinga entre mayúsculas y minúsculas (o acentos, u otras reglas de comparación específicas de la cultura/localidad) , entonces use Intl y/o localeCompare .

about 4 years ago · Juan Pablo Isaza Report

0

if (["good", "beautiful", "pretty", "excellent", "superb", "spectacular"].includes(a)) {...}
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!