Loop through two arrays

Hi, im new in Postman. I have two arrays for example var a = [β€œx”,β€œY”] and var b = [β€œ1”,β€œ2”] i’d like to send request for each combiination something like for loop in for loop. I can send requests with one array but i can’t do it with two arrays.
What is easiest way to do it?
Thanks in advance.


First of all, line 3 in what I’m assuming is your pre-request script looks incorrect.

I’m guessing that states is the outer loop and zip codes is the inner loop.

My recommendation is to not have two loops for something you can do with one loop if you format your input data appropriately. I suggest creating an array of objects for your input data.

Something similar to the following…

[
    {
        "Palm Beach": "33411"
    },
    {
        "Palm Beach": "33428"
    },
    {
        "Charlotte": "33446"
    },
    {
        "Charlotte": "33476"
    },
    {
        "Collier": "33982"
    }
]

This way, when you run the array shift you will get both elements in an object in your current variable and can do something similar to the following to retrieve the data that you are going to save as varibles for the request.

let currentObject = pm.environment.get("testData");
console.log(currentObject);

let currentState = Object.keys(currentObject); // should return  the keyname
let currentZip = currentObject[currentState]; // should return the value

console.log(currentState);
console.log(currentZip);

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.