# Get output for each item from a list of items

**URL:** <https://community.postman.com/t/get-output-for-each-item-from-a-list-of-items/55984>\
**Category:** Help Hub\
**Tags:** tests, variables, collections\
**Created:** [December 27, 2023, 4:45am UTC](https://community.postman.com/t/get-output-for-each-item-from-a-list-of-items/55984 "2023-12-27T04:45:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anchalsingh12](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@anchalsingh12](https://community.postman.com/u/anchalsingh12)\
**Post date:** [December 27, 2023, 4:45am UTC](https://community.postman.com/t/get-output-for-each-item-from-a-list-of-items/55984/1 "2023-12-27T04:45:46Z")

</div>

I have two API calls in the same collection. I got a list of alerts from my previous API call. Now in my second API call, I want to use each alert from the list to get more information on it. Now this is where I am facing a problem, the second API call keeps on picking the first element of the list and doesn’t go through each element.  
I tried the ‘Shift’ function and for loop, but both are not working.

Here is my code:  
**1st API script**  
**Get {{URL}}/alerts-list**  
**Tests**

```auto
a = JSON.parse(responseBody)
pm.collectionVariables.set("ID", a);

```

=\> got a list of alerts here successfully in collection variable ID

**2nd API script - detail**  
**Get {{URL}}/alerts-info/{{alertIDnow}}/**  
**Pre-request scripts**

```auto
const Aid = pm.collectionVariables.get("ID")
pm.collectionVariables.set("alertIDnow", Aid.shift());
postman.setNextRequest('detail');

```

=\> detail is the name of the 2nd API script. When using a collection runner it only fetches the first element from the Aid list and doesn’t iterate through the list.

Please help me out. I am using Postman web application. Been stuck on this for days.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [December 27, 2023, 8:40am UTC](https://community.postman.com/t/get-output-for-each-item-from-a-list-of-items/55984/2 "2023-12-27T08:40:37Z")

</div>

Hey @anchalsingh12 👋

Welcome to the Postman community! :postman:

This recent thread might give you some scripting inspiration, sound like you’re getting into the same issue with selecting the first item each time.

> [@collectionVariables returns value by reference or by value](https://community.postman.com/t/collectionvariables-returns-value-by-reference-or-by-value/55592):
>
> Hi community people, I am trying to simulate a collection which has 2 request Get ProductId’s Post Request: Place Orders using productId’s returned from 1st response [image] I stored all productId’s from response1 in an array in a local variable and later set variables in 2 scopes as below Case1: pm.variables.set Case2: pm.collectionVariables.set in each case, I am trying to get one productId from the given array and Place Order Array automatically updated if I make…

---

<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:** [December 30, 2023, 8:40am UTC](https://community.postman.com/t/get-output-for-each-item-from-a-list-of-items/55984/3 "2023-12-30T08:40:56Z")

</div>

Closed on December 30, 2023, 8:40am UTC

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