Run the collection using array of objects properties as variables

Hello community,
I need some help. Below is attached my code, the problem is accessing to array’s objects properties and using them as variables.

I have generated an array of objects. It is big array and I should run the collection with a different dataset in each run until the size of the array.
First of all You can run the pre-request script to get the array of objects, it’s something like this:

let requestsArray = [{"r_type":"01", "r_target":"1", "u_range":"1"},{"r_type":"02", "r_target":"11", "u_range":"54"},{...}, ...]

I have three properties in my array’s objects - "r_type", "r_target", "u_range".

My request body is x-www-form-urlencoded and looks like this:

<ReportType><![CDATA[{{r_type}}]]></ReportType>
<RequestTarget>{{r_target}}</RequestTarget>
<UsageRange>{{u_range}}</UsageRange>

How can I access to the objects within array and use its properties as variables ?

Pre-request script:

let global1 = ['01', '02', '03', '05']; 
let global2 = [1, 3, 4, 11];
let global3 = {};
global3['1'] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
global3['3'] = [31];
global3['4'] = [34];
global3['11'] = [54];

let requestsArray = [];
for (let u = 0; u < global1.length; u++)
{
    for (let j = 0; j < global2.length; j++)
    {
        for (let x = 0; x < global3[global2[j]].length; x++)
        {
            const obj = {};
            obj.r_type = global1[u];
            obj.r_target = global2[j];
            obj.u_range = global3[global2[j]][x];
            requestsArray.push(obj);
        }
    }
}
console.log(requestsArray);
let f = pm.globals.get("f");
if (!f || f.length == 0) {
    f = requestsArray;
}

let current = f.shift();
pm.globals.set("trio", current);
pm.globals.set("f", f);

Test script :

const f = pm.globals.get("f");

if (f && f.length > 0){
    postman.setNextRequest(pm.info.requestName);
} else {
    postman.setNextRequest(null);
}

You’re saving the current iteration in a trio variable, but it looks like you’re accessing the variables in the request body with different names. Have you tried adding this to the bottom of your pre-request script?

let current = f.shift();
pm.variables.set('r_type', current.r_type);
pm.variables.set('r_target', current.r_target);
pm.variables.set('u_range', current.u_range);

Also, your script is doing way too much work every time. It can be short circuited at the very beginning by moving your if statement at the very beginning. So your script could become:

let f = pm.globals.get("f");
if (!f || f.length == 0) {
    let global1 = ['01', '02', '03', '05']; 
    let global2 = [1, 3, 4, 11];
    let global3 = {};
    global3['1'] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
    global3['3'] = [31];
    global3['4'] = [34];
    global3['11'] = [54];
    
    let requestsArray = [];
    for (let u = 0; u < global1.length; u++)
    {
        for (let j = 0; j < global2.length; j++)
        {
            for (let x = 0; x < global3[global2[j]].length; x++)
            {
                const obj = {};
                obj.r_type = global1[u];
                obj.r_target = global2[j];
                obj.u_range = global3[global2[j]][x];
                requestsArray.push(obj);
            }
        }
    }
    f = requestsArray;
}

const current = f.shift();
pm.globals.set("trio", current);
pm.globals.set("f", f);
pm.variables.set('r_type', current.r_type);
pm.variables.set('r_target', current.r_target);
pm.variables.set('u_range', current.u_range);

Last point of note, I’d highly recommend nenaming your variables to something meaningful. Anyone who is not you is not going to be able to maintain this long term because your variables have no intuitive meaning and you really need to dig into the code to understand what is going on.

Good luck!

Thanks @allen.helton. This is a perfect solution. Array of objects now is working in a way as I wanted. Thanks again and again for your help.
Appreciate it :clap: :clap: :clap:

1 Like

Happy to help! Glad we were able to work through it.