# Function with Two loops

**URL:** <https://community.n8n.io/t/function-with-two-loops/12066>\
**Category:** Questions\
**Tags:** node\
**Created:** [March 10, 2022, 12:30pm UTC](https://community.n8n.io/t/function-with-two-loops/12066 "2022-03-10T12:30:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hasnaa\_SOUIBA](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/hasnaa_souiba/32/11138_2.png) [@Hasnaa\_SOUIBA](https://community.n8n.io/u/Hasnaa_SOUIBA)\
**Post date:** [March 10, 2022, 12:30pm UTC](https://community.n8n.io/t/function-with-two-loops/12066/1 "2022-03-10T12:30:18Z")

</div>

Hi,

How can i make a function with a loop inside another loop - (i.e. the items come from two different nodes)

- The first loop I want to do **(line 16)** does not work

**ERROR:** Cannot read property ‘Get InvoiceItemLines’ of undefined [Line 16].

- The second loop I want to do on **price** (knowing that the items of this item come from another node)

**Code:**

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [March 10, 2022, 12:37pm UTC](https://community.n8n.io/t/function-with-two-loops/12066/2 "2022-03-10T12:37:26Z")

</div>

Hey @Hasnaa_SOUIBA, that’s more of a general JavaScript question seeing you want to use a function tbh. Here’s a quick example showing a loop inside a loop using n8n’s Function node:

> **Example Workflow**
>

This will loop through the items returned by the first node (a few names) and then for each of these items loop through all items returned by the second node (a bunch of activities):

 ![image](https://europe1.discourse-cdn.com/n8n/original/2X/a/a831577d1d0dea27f86f688fc83ca8c20e63a22e.png)

Is this what you had in mind?

---

<div class="post-metadata">

**Author:** ![Hasnaa\_SOUIBA](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/hasnaa_souiba/32/11138_2.png) [@Hasnaa\_SOUIBA](https://community.n8n.io/u/Hasnaa_SOUIBA)\
**Post date:** [March 10, 2022, 12:48pm UTC](https://community.n8n.io/t/function-with-two-loops/12066/3 "2022-03-10T12:48:59Z")

</div>

Thanks for your help @MutedJam

I’m going to test this - (I just updated my post with the code)
