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

159
Views
Obtenga cada variable de la clase en un bucle for

Estoy creando un intérprete que lee

 variable1: 2 variable2: 400 variable3: 31

desde un archivo de texto e inicializa las variables en tiempo de ejecución. usando eval() y expresiones regulares

 class Rules{ getSegmentArgs_string(sgmnt, scope) { let argarray = new Array; let vardeclaration; for(let i=0; i<this.rules[sgmnt].length; i++) { let regexp = /(\w+): ([0-9\.]+)/g; for (const match of this.rules[sgmnt][i].matchAll(regexp)) { vardeclaration = (scope+match[1].toString() + " = "+match[2].toString()+";"); argarray[i] = vardeclaration; } } return argarray; } setSegment(sgmnt) { this.segment = sgmnt; } init() { this.defaultvars = this.getSegmentArgs_string(this.segment,"this.") for(let i=0; i<this.defaultvars.length; i++) { eval(this.defaultvars[i]); } delete(this.rules); delete(this.defaultvars); delete(this.segment); } constructor() { this.rules = this.dump(); } } let rules = new Rules; rules.setSegment(3); rules.init();

Sin embargo, no puedo entender cómo acceder a estas variables inicializadas en un bucle for que arroja el nombre de cada variable, así como su valor.

 for (const variables in rules) { console.log({variales}); console.log({variables}.value); }

Soy capaz de acceder al valor al hacer

 console.log(rules.variables1,rules.variables2,rules.variables3)

pero debido a la naturaleza de la tarea, necesito recorrer todas las variables sin nombrarlas en el código.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

for...in te da la clave de las propiedades del objeto. Tienes que acceder al valor con la clave

 for (const key in rules) { console.log(key, rules[key]); }
about 4 years ago · Santiago Gelvez 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!