Need help in adding nested loop condition in pre-requisite

Use case: Need to iterate through a nested set of values
example :

Users = [“User1”,“User2”]
Events = [“Event1”, “Event2”,“Event3”]

I want to iterate like
User as User1 and Event as Event1
User as User1 and Event as Event2
User as User1 and Event as Event3
User as User2 and Event as Event1
User as User2 and Event as Event2 and so on

I’ve already tried:

Tried this for a single loop, didn’t get how to script for nested loops

`let Eventnames = pm.collectionVariables.get(“Eventnames”);

if(!Eventnames || Eventnames.length == 0) {
Eventnames = [“Event1”, “Event2”, “Event3”];
}

let currentEventname = Eventnames.shift();
console.log(currentEventname)
pm.collectionVariables.set(“EventName”, currentEventname);
pm.collectionVariables.set(“Eventnames”, Eventnames);
`

@danny-dainton : can you please help to achieve this use-case

Hi @swasthik

You could try a nested for loop … Something like this;

//for each user in array
for (let x = 0; x < Users.length; x++) {
    //for each event in array
    for (let y = 0; y < Events.length; y++) {
        console.log(x + y);
    }
}
1 Like