# Define a function within a function and pass an object, forEach

**URL:** <https://discourse.nodered.org/t/define-a-function-within-a-function-and-pass-an-object-foreach/67624>\
**Category:** General\
**Created:** [12 September 2022 19:58 UTC](https://discourse.nodered.org/t/define-a-function-within-a-function-and-pass-an-object-foreach/67624 "2022-09-12T19:58:39Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![VinistoisR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinistoisr/32/5778_2.png) [@VinistoisR](https://discourse.nodered.org/u/VinistoisR)\
**Post date:** [12 September 2022 19:58 UTC](https://discourse.nodered.org/t/define-a-function-within-a-function-and-pass-an-object-foreach/67624/1 "2022-09-12T19:58:39Z")

</div>

I have an array in a flow variable like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/4798529677279481e6c836360d67d8790d82262e.png)

I want to cycle through each of the objects in the array, and send out a new message for each one, using some values from the objects to build the new message.

```auto
let machines = flow.get("machines") || [];

var iterate = function(id, name) {
    node.warn(id);
    node.warn(name);
    var url = "http://example.com/" + id + "/" + name + "/api";
    msg.url = url;
    node.send(msg.url);
};
machines.forEach(iterate(msg.id, msg.name));

```

I'm having trouble passing the array in to the function in this way. Can anyone offer hints on this process?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 September 2022 20:22 UTC](https://discourse.nodered.org/t/define-a-function-within-a-function-and-pass-an-object-foreach/67624/2 "2022-09-12T20:22:53Z")

</div>

> [@VinistoisR](#):
>
> ```auto
> machines.forEach(iterate(msg.id, msg.name));
> 
> ```

```auto
machines.forEach((arr) => {
    iterate(arr[0].id, arr[0].name)
});

```

If i read your payload image correctly.

> **[Array.prototype.forEach() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)**
>
> The forEach() method of Array instances executes a provided function once
> for each array element.

[edit] edit fixed typo.

---

<div class="post-metadata">

**Author:** ![VinistoisR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinistoisr/32/5778_2.png) [@VinistoisR](https://discourse.nodered.org/u/VinistoisR)\
**Post date:** [12 September 2022 20:39 UTC](https://discourse.nodered.org/t/define-a-function-within-a-function-and-pass-an-object-foreach/67624/3 "2022-09-12T20:39:44Z")

</div>

thank you, here's where I'm at now:

```auto
let machines = flow.get("machines") || [];
node.warn(machines);

var iterate = function(id, name) {
    node.warn(id);
    node.warn(name);
    var url = "http://example.com/" + id + "/" + name + "/api";
    msg.url = url;
    node.warn(msg.url);
};

machines.forEach((arr) => {
    iterate(arr[0].id, arr[0].name)
});

return msg;

```

I only get the array out of the first node.warn, so it would appear nothing is making it into the iterate function:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/965fcabfc5bec75cb4a87fe9fbb202b413d07614.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 September 2022 20:59 UTC](https://discourse.nodered.org/t/define-a-function-within-a-function-and-pass-an-object-foreach/67624/4 "2022-09-12T20:59:51Z")

</div>

Show us machines array.  
I would also declare your function with const not var, and use let inside your function, rather than var.  
[edit]  
The first post showed an array of single arrays containing an object,  
Your new image shows an array of objects.  
With and array of objects it would be.

```auto
let machines = flow.get("machines") || [];
// machines =[{id:1,name:"one"},{id:2,name:"two"}];
node.warn(machines);

const iterate = function(id, name) {
    node.warn(id);
    node.warn(name);
    let url = "http://example.com/" + id + "/" + name + "/api";
    msg.url = url;
    node.warn(msg.url);
};

machines.forEach((obj) => {
    iterate(obj.id, obj.name)
});

return msg;

```

---

<div class="post-metadata">

**Author:** ![VinistoisR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinistoisr/32/5778_2.png) [@VinistoisR](https://discourse.nodered.org/u/VinistoisR)\
**Post date:** [12 September 2022 21:32 UTC](https://discourse.nodered.org/t/define-a-function-within-a-function-and-pass-an-object-foreach/67624/5 "2022-09-12T21:32:00Z")

</div>

the array is in the image I just posted in my last reply, here is is in the flow context storage:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/4/b4dd164eea5be4c8e2a6928b8393564241a59b87.png)

I take your advice of the const / let... but I don't pretend to know why!

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 September 2022 21:33 UTC](https://discourse.nodered.org/t/define-a-function-within-a-function-and-pass-an-object-foreach/67624/6 "2022-09-12T21:33:51Z")

</div>

> [@VinistoisR](#):
>
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/4798529677279481e6c836360d67d8790d82262e.png)
> 
> image340×656 19.7 KB

Which is not the same as your original image, the goal posts moved.

---

<div class="post-metadata">

**Author:** ![VinistoisR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinistoisr/32/5778_2.png) [@VinistoisR](https://discourse.nodered.org/u/VinistoisR)\
**Post date:** [12 September 2022 21:35 UTC](https://discourse.nodered.org/t/define-a-function-within-a-function-and-pass-an-object-foreach/67624/7 "2022-09-12T21:35:24Z")

</div>

that's odd. I didn't do anything to change that...

---

<div class="post-metadata">

**Author:** ![VinistoisR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinistoisr/32/5778_2.png) [@VinistoisR](https://discourse.nodered.org/u/VinistoisR)\
**Post date:** [12 September 2022 21:45 UTC](https://discourse.nodered.org/t/define-a-function-within-a-function-and-pass-an-object-foreach/67624/8 "2022-09-12T21:45:06Z")

</div>

> [@E1cid](#):
>
> With and array of objects it would be.
> 
> ```auto
> 
> ```

lovely... this works, thank you. In one example you used 'arr' and in the next you used 'obj'. Am I correct that you could have used 'foo', or anything, but you are using those specifically just for code clarity?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 September 2022 21:50 UTC](https://discourse.nodered.org/t/define-a-function-within-a-function-and-pass-an-object-foreach/67624/9 "2022-09-12T21:50:42Z")

</div>

It's a naming convention as the first foreEach was creating an array for each loop so I named it arr,. The second example looped through and was producing an object so I named it obj. That way you know later when you look back or bug fix, you know you expected an array or object.

---

<div class="post-metadata">

**Author:** ![VinistoisR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinistoisr/32/5778_2.png) [@VinistoisR](https://discourse.nodered.org/u/VinistoisR)\
**Post date:** [12 September 2022 21:54 UTC](https://discourse.nodered.org/t/define-a-function-within-a-function-and-pass-an-object-foreach/67624/10 "2022-09-12T21:54:02Z")

</div>

so any idea why the data changed formats like that? This comes straight from the API response... so I can only guess they made an API change on me?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 September 2022 22:03 UTC](https://discourse.nodered.org/t/define-a-function-within-a-function-and-pass-an-object-foreach/67624/11 "2022-09-12T22:03:20Z")

</div>

There could be a object reference issue as forech works on each item and the context is referenced to machines. But I'm not seeing why msgid dissappeared and serial remained. If it is an issue you could clone machines using [RED.utils.cloneMessage(machines).ForEach(...](https://nodered.org/docs/api/modules/v/0.20.0/@node-red_util_util.html#.cloneMessage)

call the api and show us what it returns maybe they changed their output.

Are others working on this machine?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [11 November 2022 22:03 UTC](https://discourse.nodered.org/t/define-a-function-within-a-function-and-pass-an-object-foreach/67624/12 "2022-11-11T22:03:23Z")

</div>

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