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

342
Views
¿Cómo obtener el valor de 'índice' y ponerlo en una variable global?

¿Cómo pongo el valor de 'índice' a una variable global? El valor del índice se muestra solo si está dentro del ciclo forEach, ¿hay alguna forma de sacarlo y ponerlo en una variable global?

 stars.forEach((star,index) =>{ star.addEventListener('click', ()=>{ stars.forEach((star,idx) =>{ if(idx < index + 1){ star.classList.add('active'); } else { star.classList.remove('active'); } console.log(index); // this is working }); }); }); console.log(index); // this is NOT working

Por favor revise gracias!

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

0

En primer lugar, esto no parece ser una buena idea, pero aquí tienes:

 var globalIdx = -1 function fnc() { var functionIdx = -1 stars.forEach((star,index) =>{ star.addEventListener('click', ()=>{ stars.forEach((star,idx) =>{ if(idx < index + 1){ star.classList.add('active'); } else { star.classList.remove('active'); } console.log(index); // this is working globalIdx = index; functionIdx = index; }); }); }); console.log(index); // will not work console.log(globalIdx); // will work console.log(functionIdx); // will work } console.log(globalIdx); // will work console.log(functionIdx); // will not work

Simplemente elija uno entre functionIdx y globalIdx según lo que necesite.

Espero que esto ayude :)

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!