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

219
Views
En el bucle de solicitudes de búsqueda, ¿cómo pasa el valor "i" a la carga útil del cuerpo que se ha definido como plantilla literal fuera del bucle?

Quiero realizar varias solicitudes de recuperación, cada solicitud tiene una carga diferente, studentID de estudiante en este ejemplo. Cada ciclo cambia el ID de estudiante, pero la carga útil se define como una variable literal de plantilla fuera del ciclo para facilitar la lectura. ¿Hay alguna manera de obtener el valor del iterador de bucle para la carga útil (definida fuera)?

 var payload=`{"studentID": ${studentID}}` //expecting studentID to be iterated in loop var myInit = { method: 'POST', headers: { 'Accept': 'application/json, text/plain, */*', 'Content-Type': 'application/json' }, body: payload }; for(var studentID = 0; studentID <= 10; studentID++) { fetch(URI, myInit) .then( r => r.json() ) .then( r => console.log(r) .catch(e => console.log(e)); }

Los valores de ID de estudiante (0,1,2,3...) no pueden entrar en la plantilla de payload de myInit.

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

0

Haz una función en su lugar.

 const makeInit = studentID => ({ method: 'POST', headers: { 'Accept': 'application/json, text/plain, */*', 'Content-Type': 'application/json' }, body: `{"studentID": ${studentID}}` }); for(var studentID = 0; studentID <= 10; studentID++) { fetch(URI, makeInit(studentID))

Tal vez no encadenar manualmente.

 const makeInit = studentID => ({ method: 'POST', headers: { 'Accept': 'application/json, text/plain, */*', 'Content-Type': 'application/json' }, body: JSON.stringify({ studentID }) }); for(var studentID = 0; studentID <= 10; studentID++) { fetch(URI, makeInit(studentID))
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!