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

370
Views
La función declarada en un bucle contiene referencias no seguras a variable(s)

Mi código comprueba si un usuario está disponible. Vea el fragmento a continuación:

 const users = ['user1', 'user2', 'user3', 'user4'] const usersToAdd = 2 const getRandomWorker = (userArray) => { return userArray[Math.floor(userArray.length * Math.random())] } const availableUsers = [] for (let i = 0; i < usersToAdd; i += i) { let randomWorker = getRandomWorker(users) let didAddWorker = false while (!didAddWorker) { if (checkIfUserAvailable(randomWorker)) { availableUsers.push(randomWorker) users = users.filter((user) => user !== randomWorker) didAddWorker = true } else if (!users.length) { didAddWorker = true } else { users = users.filter((user) => user !== randomWorker) } } }

Mi único problema es que contiene referencias inseguras a variables porque aparece el siguiente error:

 Function declared in a loop contains unsafe references to variable(s) randomWorker.

He buscado y jugueteado con mi código, pero no puedo deshacerme del error. Ya no se donde mirar.

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

0

Como sugirió TJ Crowder, tenía que hacer una función fuera del bucle:

 const filterOut = (array, target) => array.filter(element => element !== target);

Eso lo arregló. Un poco extraño pero puedo seguir sabiendo. ¡Gracias!

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!