简体   繁体   中英

Javascript loop that executes a post but it only executes the last value from the loop

I'm trying to execute multiple ids and add it in an object to be executed. so this is from an event button function:

{
for(var i =0; i< selectrows.length; i++){
   Action["trackid"] = selectrows[i].innerText.replace(/(^\d+)(.+$)/i, '$1');

   researchService.postExecuteAction(Action)
     .then(function(result){
     },function error(result){
     });
}
}

So for example the trackids are 111, 112, 113. It will only execute trackid 113 for three times. I would like to execute 111, 112 as well. I've tried many different ways of closure to handle these but it didn't work. I might be doing something wrong. Please help. Thank you.

Probably because you reassign the Action["trackid"] on the same object.

Do something like:

{
for(var i =0; i< selectrows.length; i++){
   const action = { ...Action }
   action["trackid"] = selectrows[i].innerText.replace(/(^\d+)(.+$)/i, '$1');

   researchService.postExecuteAction(action)
     .then(function(result){
     },function error(result){
     });
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM