Loop through array!
Posted: Sat Feb 27, 2021 1:01 am
I am little bit confused how Switch scripting works. I read in a post that the when the job arrives to a scripting step, the sendToSingle function does not execute until script is fully done. What I am trying to achieve is that I have jobs in form of xml and within the xml theres a metadata contain a list of urls and I want to loop through those links and send each of them as a job and attach privatedata for alter call to be downloaded.
The issue I am encountering is that is that when I loop through array with "For" method, it always returns the last element of the array and it send the jobs to next outgoing flow with the last element of the array in all jobs
Let's for example say I have an array of ["Url1", "Url2","Url3"] , Switch sends 3 jobs and all of them carry privatedata of "Url3" while I want 3 jobs and each of them have a privatedata if url1 for 1st, url2 for 2nd and url3 for 3rd.
I tried different ways by adding index to the loop so the index will increase with every loop but I still get same result .
Below is one of the codes that that I tried
Any idea how to achieve looping through the Urls in the array and send amount of job based on how many urls exists in the array and each job MUST carry one url out of the array and have it as privatedata?
Thank you
The issue I am encountering is that is that when I loop through array with "For" method, it always returns the last element of the array and it send the jobs to next outgoing flow with the last element of the array in all jobs
Let's for example say I have an array of ["Url1", "Url2","Url3"] , Switch sends 3 jobs and all of them carry privatedata of "Url3" while I want 3 jobs and each of them have a privatedata if url1 for 1st, url2 for 2nd and url3 for 3rd.
I tried different ways by adding index to the loop so the index will increase with every loop but I still get same result .
Below is one of the codes that that I tried
async function jobArrived(s,flowElement,job){
let links= await parseXPathAsString("file","/JSON/array/urls",job);
let cleanURLs= links.match(/\bhttps?:\/\/\S+/gi);
for(let i = 0; cleanURLs.length > i; i++) {
if (cleanURLs.indexOf('dropbox') != -1){
cleanURLs = cleanURLs.replace('dl=0', 'dl=1');
}
await job.setPrivateData("Link",cleanURLs);
await job.sendToSingle(job.get());
}
}
Any idea how to achieve looping through the Urls in the array and send amount of job based on how many urls exists in the array and each job MUST carry one url out of the array and have it as privatedata?
Thank you