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

167
Views
jscodeshift convertir forEach a bucle for

Soy nuevo en jscodeshift y AST, pero estoy tratando de hacer una conversión para mis bucles forEach existentes y convertirlos en bucles regulares.

Quiero encubrir lo siguiente:

 [ `foo`, `bar` ].forEach(test => { console.log(test); });

A esto:

 for(const test of [`foo`, `bar`]) { console.log(test); }
 export default (file, api) => { const j = api.jscodeshift; const root = j(file.source); // What do I do here to transform the forEach to a regular for loop? return root.toSource(); };

He estado revisando algunos de los documentos y buscando, pero no puedo encontrar una manera de hacer esto.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

El es lo que estaba buscando.

 root.find(j.CallExpression, { callee : { property : { name : "forEach" } } }) .replaceWith(path => { // Hangles foo.forEach() and [1,2].forEach() const expression = path.value.callee.object.name ? j.identifier(path.value.callee.object.name) : j.arrayExpression(path.value.callee.object.elements); return j.forOfStatement( j.variableDeclaration( "const", path.value.arguments[0].params ), expression, path.value.arguments[0].body ) });
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!