# Post Looping through an Array not working

**URL:** <https://community.postman.com/t/post-looping-through-an-array-not-working/40758>\
**Category:** Help Hub\
**Created:** [November 3, 2022, 6:38pm UTC](https://community.postman.com/t/post-looping-through-an-array-not-working/40758 "2022-11-03T18:38:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![otrjgn](https://avatars.discourse-cdn.com/v4/letter/o/f17d59/32.png) [@otrjgn](https://community.postman.com/u/otrjgn)\
**Post date:** [November 3, 2022, 6:38pm UTC](https://community.postman.com/t/post-looping-through-an-array-not-working/40758/1 "2022-11-03T18:38:10Z")

</div>

I’ve followed the example but I’m unable to get this pre-request script to loop through my array. It’s only posting the first item.

**Body**  
{  
“carrierpkey”:“{{clientpkey}}”  
}

**Pre-requestScript**  
var pkeys = pm.environment.get(“pkeys”);

if(!pkeys){  
pkeys =[31922, 456789, 75225];  
}

var currentpkey = pkeys.shift();  
pm.environment.set(“clientpkey”,currentpkey);  
pm.environment.set(“pkeys”, pkeys);

**Tests**  
var pkeys = pm.environment.get(“pkeys”);

if(pkeys && pkeys.length \> 0){  
postman.setNextRequest(“My URL Here”)

} else{  
postman.setNextRequest(null);  
}

---

<div class="post-metadata">

**Author:** ![otrjgn](https://avatars.discourse-cdn.com/v4/letter/o/f17d59/32.png) [@otrjgn](https://community.postman.com/u/otrjgn)\
**Post date:** [November 3, 2022, 7:07pm UTC](https://community.postman.com/t/post-looping-through-an-array-not-working/40758/2 "2022-11-03T19:07:50Z")

</div>

I figured it out. For the “run” process I need to add the iterations amount. This doesn’t seem correct, why doesn’t it just loop based on the array length?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 4, 2022, 4:10pm UTC](https://community.postman.com/t/post-looping-through-an-array-not-working/40758/3 "2022-11-04T16:10:21Z")

</div>

I can’t see a loop here. I can see a couple of IF statements.

What do you mean by the “run” process. Is this set to loop?

Do you mean the shift line which should just remove and return the first element of the array.

Have a look at this link which has an example of a for and forEach loop if that is what you need.

> [@Run the same test through all arrays objects](https://community.postman.com/t/run-the-same-test-through-all-arrays-objects/40782):
>
> Hey! Is there a way to run the same test script through all array objects? I have that pm.test that i want to run through all array objects, that in that case is 82, but it might be more or it might be less. Is there a way to this? Thanks a bunch!

---

<div class="post-metadata">

**Author:** ![otrjgn](https://avatars.discourse-cdn.com/v4/letter/o/f17d59/32.png) [@otrjgn](https://community.postman.com/u/otrjgn)\
**Post date:** [November 4, 2022, 6:40pm UTC](https://community.postman.com/t/post-looping-through-an-array-not-working/40758/4 "2022-11-04T18:40:44Z")

</div>

Isn’t this the loop? Get the length of the array, setnextrequest until the length goes to zero

if(pkeys && pkeys.length \> 0){  
postman.setNextRequest(“My URL Here”)

} else{  
postman.setNextRequest(null);  
}

I’m using this sample but not sure what I’m missing  
[https://www.youtube.com/watch?v=sZKoBQ1HpKw](https://www.youtube.com/watch?v=sZKoBQ1HpKw)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 7, 2022, 12:59pm UTC](https://community.postman.com/t/post-looping-through-an-array-not-working/40758/5 "2022-11-07T12:59:05Z")

</div>

Is that first setNextRequest basically the same request looping back on itself.

When you say Tests, is this in the Tests tab on the request so it runs after the request.

I sort of get the logic. No idea if it actually works. I don’t have time today but may try and replicate this tomorrow as I can see where this could be useful.

---

<div class="post-metadata">

**Author:** ![otrjgn](https://avatars.discourse-cdn.com/v4/letter/o/f17d59/32.png) [@otrjgn](https://community.postman.com/u/otrjgn)\
**Post date:** [November 7, 2022, 1:53pm UTC](https://community.postman.com/t/post-looping-through-an-array-not-working/40758/6 "2022-11-07T13:53:58Z")

</div>

> [@otrjgn](#):
>
> if(pkeys && pkeys.length \> 0){  
> postman.setNextRequest(“My URL Here”)
> 
> } else{  
> postman.setNextRequest(null);  
> }

This is in the “test” tab. And yes, the setNextRequest should iterate through the length of the array and post accordingly.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 9, 2022, 2:17pm UTC](https://community.postman.com/t/post-looping-through-an-array-not-working/40758/7 "2022-11-09T14:17:07Z")

</div>

@otrjgn

I finally got round to testing this and it does work with some slight modifications.

I didn’t need to set the iterations to 3 in the run options.

This is the pre-request script.

```auto
var array = pm.collectionVariables.get("pkeys");

if(array.length === 0){
    array = [31922, 456789, 75225];
}

pm.test("pre-req array is not empty", () => {
    pm.expect(array).to.be.an("array").that.is.not.empty; 
});

// console.log("pre-req array: " + array);

var currentpkey = array.shift();
// console.log("pre-req current key: " + currentpkey);
// console.log("pre-req updated array " + array);

pm.collectionVariables.set("clientpkey",currentpkey);
pm.collectionVariables.set("pkeys", array);

```

This is the tests tab script.

```auto
var array = pm.collectionVariables.get("pkeys");

var expectedResponse = pm.collectionVariables.get("clientpkey");
var actualResponse = parseInt(pm.response.json().args.test);

pm.test(`tests-tab clientpkey = ${expectedResponse}`, () => {
    pm.expect(actualResponse).to.eql(expectedResponse); 
});

if (array.length > 0){
postman.setNextRequest("Request1");

} else {
postman.setNextRequest(null);
}

```

I’m just using the Postman echo URL for the test including the clientpkey variable which it will echo back to you. I’ve included an assertion in the tests tab that check the response included the key that was sent.

```auto
https://postman-echo.com/get?test={{clientpkey}

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/5/d/5dddac4d787b8df5ecec46cd6d7707ddd9893cdf.png)

You can see in the results that it ran three times, but in a single iteration.

I can see some uses for this approach, where you want a particular request to loop through data from a previous request.

---

<div class="post-metadata">

**Author:** ![otrjgn](https://avatars.discourse-cdn.com/v4/letter/o/f17d59/32.png) [@otrjgn](https://community.postman.com/u/otrjgn)\
**Post date:** [March 22, 2023, 2:24pm UTC](https://community.postman.com/t/post-looping-through-an-array-not-working/40758/8 "2023-03-22T14:24:09Z")

</div>

@michaelderekjones Thanks for the help. All of a sudden I’ve started getting this error and now nothing works. Any idea on why this is happening?

ProcessEntityChange failed: Could not convert string to integer: null. Path ‘carrierpkey’, line 2, position 22.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [March 22, 2023, 2:36pm UTC](https://community.postman.com/t/post-looping-through-an-array-not-working/40758/9 "2023-03-22T14:36:11Z")

</div>

I noticed this a while back.

Something changed, and you need to stringify when storing the array, and parse it when retrieving it.

Not sure when it changed though as this was working at one point.

> Pre-request script.

```auto
var array = JSON.parse(pm.collectionVariables.get("pkeys"));

if(array.length === 0){
    array = [31922, 456789, 75225];
}

pm.test("pre-req array is not empty", () => {
    pm.expect(array).to.be.an("array").that.is.not.empty; 
});

// console.log("pre-req array: " + array);

var currentpkey = array.shift();
// console.log("pre-req current key: " + currentpkey);
// console.log("pre-req updated array " + array);

pm.collectionVariables.set("clientpkey",currentpkey);
pm.collectionVariables.set("pkeys", JSON.stringify(array));

```

> Tests tab

```auto
var array = JSON.parse(pm.collectionVariables.get("pkeys"));

var expectedResponse = pm.collectionVariables.get("clientpkey");
var actualResponse = pm.response.json().args.test;

pm.test(`tests-tab clientpkey = ${expectedResponse}`, () => {
    pm.expect(actualResponse).to.eql(expectedResponse); 
});

if (array.length > 0){
    postman.setNextRequest("Request1");

    } else {
        postman.setNextRequest(null);
    }

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 24, 2023, 2:57pm UTC](https://community.postman.com/t/post-looping-through-an-array-not-working/40758/10 "2023-08-24T14:57:08Z")

</div>

Closed on August 24, 2023, 2:57pm UTC

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