Looping through array variable

I am trying to loop through and array for each iteration.

I have below array variable in my environment host variable section:
UserId = ā€œMargin_Userā€,ā€œQFC_LOB_Userā€,ā€œQFC_Userā€

In my Test code, i am retrieving with below:
let Users = pm.environment.get(ā€˜UserIdā€™);

but below length gives me 39 total length instead of 3.
pm.test(Users.length);

This is causing issue with reading the variables:

can someone help me on this?

Thanks
Sachin

Hey sachin,

Welcome to posman community.
The issue is when you store something in environment or from test script it is stored as a string thatā€™s the reason you get the length as 39.

There are 2 ways to handle this

  1. Use the split function to store it in a array like this
let Users = pm.environment.get(ā€˜UserIdā€™);
let UsersArr = Users.split(",");

After this you can access values under UsersArr as UsersArr[0] & the length will be 3.

  1. If you are extracting users from a API call, either chain it to next request or save each value individually rather than a array.

Let me know if this works for you.
Regards,
Skandh

Thanks Skandh. This helped. Really appreciate timely solution.