# Issue with a loop in flows

**URL:** <https://community.postman.com/t/issue-with-a-loop-in-flows/57852>\
**Category:** Help Hub\
**Tags:** loop, postman-flows\
**Created:** [February 20, 2024, 9:54am UTC](https://community.postman.com/t/issue-with-a-loop-in-flows/57852 "2024-02-20T09:54:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![suhi10jr](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@suhi10jr](https://community.postman.com/u/suhi10jr)\
**Post date:** [February 20, 2024, 9:54am UTC](https://community.postman.com/t/issue-with-a-loop-in-flows/57852/1 "2024-02-20T09:54:02Z")

</div>

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

Why doesn’t this work?  
It only loops once and then just ends the flow.

---

<div class="post-metadata">

**Author:** ![flows-daniel](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/flows-daniel/32/25516_2.png) [@flows-daniel](https://community.postman.com/u/flows-daniel)\
**Post date:** [February 20, 2024, 8:28pm UTC](https://community.postman.com/t/issue-with-a-loop-in-flows/57852/2 "2024-02-20T20:28:36Z")

</div>

Hi @suhi10jr

Welcome to the forums!

The Flows variables X and Y you have will only fire one time on the first iteration. Flows variables don’t currently work in loops, although we are going to be fixing this in the near future.

Based on what I can see from your flow, it looks like you’re trying to loop through Y until you find a value that matches X and then output the index.

Try the below flow changes as it will send all the info on each iteration of the loop so you don’t have the issue with the variables:

 ![Screenshot 2024-02-20 at 12.21.35 PM](https://us1.discourse-cdn.com/getpostman/original/3X/4/0/40cf170910e4ae356c6f062ac37a25d9f8b04083.png)

a record (JSON) is constructed, the index value is incremented by 1, and then the check is performed. If it’s false, the record is re-sent to the evaluate block, if it’s true, then the record is output and you can select the index.

Let me know if this works for you.

---

<div class="post-metadata">

**Author:** ![suhi10jr](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@suhi10jr](https://community.postman.com/u/suhi10jr)\
**Post date:** [February 20, 2024, 10:20pm UTC](https://community.postman.com/t/issue-with-a-loop-in-flows/57852/3 "2024-02-20T22:20:36Z")

</div>

Thank you! Works well, just very slow. Is there any more optimized way to do this?

---

<div class="post-metadata">

**Author:** ![flows-daniel](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/flows-daniel/32/25516_2.png) [@flows-daniel](https://community.postman.com/u/flows-daniel)\
**Post date:** [February 21, 2024, 1:38am UTC](https://community.postman.com/t/issue-with-a-loop-in-flows/57852/4 "2024-02-21T01:38:19Z")

</div>

Hi @suhi10jr

You could use the [$filter](https://learning.postman.com/docs/postman-flows/flows-query-language/function-reference/#filter) function to get the matching element (if it exists) but it wouldn’t return the index if you need that.

---

<div class="post-metadata">

**Author:** ![suhi10jr](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@suhi10jr](https://community.postman.com/u/suhi10jr)\
**Post date:** [February 21, 2024, 8:03am UTC](https://community.postman.com/t/issue-with-a-loop-in-flows/57852/5 "2024-02-21T08:03:29Z")

</div>

That could work, but i tried the filter function and just nothing happened. I was probably doing something wrong.

---

<div class="post-metadata">

**Author:** ![flows-daniel](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/flows-daniel/32/25516_2.png) [@flows-daniel](https://community.postman.com/u/flows-daniel)\
**Post date:** [February 21, 2024, 6:47pm UTC](https://community.postman.com/t/issue-with-a-loop-in-flows/57852/6 "2024-02-21T18:47:24Z")

</div>

Try this, it will output a list of the value found, if it finds it more than once, it will be a list with duplicate elements:

 ![Screenshot 2024-02-21 at 10.46.04 AM](https://us1.discourse-cdn.com/getpostman/original/3X/f/d/fd5c22e3a8d2d779288b1c1891b7831e8379fb82.png)

---

<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:** [March 22, 2024, 6:48pm UTC](https://community.postman.com/t/issue-with-a-loop-in-flows/57852/7 "2024-03-22T18:48:25Z")

</div>

Closed on March 22, 2024, 6:48pm UTC

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