# Looping through array variable

**URL:** <https://community.postman.com/t/looping-through-array-variable/28975>\
**Category:** Help Hub\
**Tags:** dynamic-variable\
**Created:** [October 21, 2021, 7:21pm UTC](https://community.postman.com/t/looping-through-array-variable/28975 "2021-10-21T19:21:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sachintss](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@sachintss](https://community.postman.com/u/sachintss)\
**Post date:** [October 21, 2021, 7:21pm UTC](https://community.postman.com/t/looping-through-array-variable/28975/1 "2021-10-21T19:21:36Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/4/4c6e8d85bdc19da495e47729dabb984a256e47d4.png)

can someone help me on this?

Thanks  
Sachin

---

<div class="post-metadata">

**Author:** ![skandh-15](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@skandh-15](https://community.postman.com/u/skandh-15)\
**Post date:** [October 22, 2021, 2:06am UTC](https://community.postman.com/t/looping-through-array-variable/28975/2 "2021-10-22T02:06:31Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![sachintss](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@sachintss](https://community.postman.com/u/sachintss)\
**Post date:** [October 22, 2021, 8:36pm UTC](https://community.postman.com/t/looping-through-array-variable/28975/3 "2021-10-22T20:36:25Z")

</div>

Thanks Skandh. This helped. Really appreciate timely solution.
