# Add variable to function

**URL:** <https://discourse.nodered.org/t/add-variable-to-function/68117>\
**Category:** General\
**Created:** [23 September 2022 06:22 UTC](https://discourse.nodered.org/t/add-variable-to-function/68117 "2022-09-23T06:22:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [23 September 2022 06:22 UTC](https://discourse.nodered.org/t/add-variable-to-function/68117/1 "2022-09-23T06:22:01Z")

</div>

I have a function node that takes in an array and spits out each object as a seperate message. This works 100% like it should:

```auto
for (var i = 0; i < msg.payload.length; i++) {
 var newMsg = {};
 newMsg.payload = msg.payload[i];
 node.send(newMsg);
}
return null;

```

Up until this point the message with the array of objects has a `msg.prefix` that is removed when the message passes through the function node. The value of `msg.prefix` is dynamic and can vary with each message.

I want to re-add `msg.prefix` as part of the function so that each separate message includes `msg.prefix` with the correct value when it comes back out of the function node.

So take the input and re-add it inside. Anyone know how?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [23 September 2022 06:43 UTC](https://discourse.nodered.org/t/add-variable-to-function/68117/2 "2022-09-23T06:43:55Z")

</div>

Unless I misunderstand the requirement, all you need is `newMsg.prefix = msg.prefix;`

---

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [23 September 2022 06:53 UTC](https://discourse.nodered.org/t/add-variable-to-function/68117/3 "2022-09-23T06:53:04Z")

</div>

That worked perfectly. For anyone wondering in the future, this is the new code:

```auto
for (var i = 0; i < msg.payload.length; i++) {
 var newMsg = {};
 newMsg.payload = msg.payload[i];
 newMsg.prefix = msg.prefix;
 node.send(newMsg);
}
return null;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 September 2022 07:05 UTC](https://discourse.nodered.org/t/add-variable-to-function/68117/4 "2022-09-23T07:05:39Z")

</div>

Have you tried feeding it through a Split node instead of your function? I suspect it will do what you want.

---

<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:** [7 October 2022 07:06 UTC](https://discourse.nodered.org/t/add-variable-to-function/68117/5 "2022-10-07T07:06:14Z")

</div>

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