# Use a variable in define msg"X" = -\> (instead of msg1, msg2, msg3, msgX in loop)

**URL:** https://discourse.nodered.org/t/use-a-variable-in-define-msg-x-instead-of-msg1-msg2-msg3-msgx-in-loop/40108
**Category:** General
**Created:** [31 January 2021 13:26 UTC](https://discourse.nodered.org/t/use-a-variable-in-define-msg-x-instead-of-msg1-msg2-msg3-msgx-in-loop/40108 "2021-01-31T13:26:28Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Toffy77cc](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@Toffy77cc](https://discourse.nodered.org/u/Toffy77cc)
#### Post date: [31 January 2021 13:26 UTC](https://discourse.nodered.org/t/use-a-variable-in-define-msg-x-instead-of-msg1-msg2-msg3-msgx-in-loop/40108/1 "2021-01-31T13:26:28Z")

</div>

Hi There 🙂  
I'm quite new and have a problem with using a variable in defining the msgX = Number... for Output in a function node. I want to replace msg1, msg2, msg3.... with msgt, msg't', {msg;t} or so to use loop counter for topic and message number. Example in the line with "\<-------" in code below

But I don't know how to write the variable as addendum for msg number....

if (msg.topic == "init"){  
msg1 = {payload:0}  
msg2 = {payload:0}  
msg3 = {payload:0}  
x = 0  
}

if (msg.payload == 1 ){x = x + 1}  
if (msg.payload === 0 && x \> 0 ){x = x - 1}

for( t = 1; t \< 2; t++){

if (msg.topic == t && msg.payload == 1 && x \< 3){msg{t} = {payload:1}} // \<-------  
if (msg.topic == t && msg.payload == 1 && x \> 2){msg1 = {payload:0}}  
if (msg.topic == t && msg.payload === 0 ) {msg1 = {payload:0}}

if (msg.topic == 2 && msg.payload == 1 && x \< 3){msg2 = {payload:1}}  
if (msg.topic == 2 && msg.payload == 1 && x \> 2){msg2 = {payload:0}}  
if (msg.topic == 2 && msg.payload === 0 ) {msg2 = {payload:0}}

if (msg.topic == 3 && msg.payload == 1 && x \< 3){msg3 = {payload:1}}  
if (msg.topic == 3 && msg.payload == 1 && x \> 2){msg3 = {payload:0}}  
if (msg.topic == 3 && msg.payload === 0 ) {msg3 = {payload:0}}

}

msg4 = {payload:x}

return [msg1, msg2, msg3, msg4];

Thanks!! 🙂

---

<div class="post-metadata">

### Author: ![softy2k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/softy2k/32/23707_2.png) [@softy2k](https://discourse.nodered.org/u/softy2k)
#### Post date: [31 January 2021 13:32 UTC](https://discourse.nodered.org/t/use-a-variable-in-define-msg-x-instead-of-msg1-msg2-msg3-msgx-in-loop/40108/2 "2021-01-31T13:32:30Z")

</div>

Some examples :

> for (t=0; t\<5; t++) msg[t] = t;

> msg["0"] = 77;

---

<div class="post-metadata">

### Author: ![Toffy77cc](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@Toffy77cc](https://discourse.nodered.org/u/Toffy77cc)
#### Post date: [31 January 2021 14:14 UTC](https://discourse.nodered.org/t/use-a-variable-in-define-msg-x-instead-of-msg1-msg2-msg3-msgx-in-loop/40108/3 "2021-01-31T14:14:30Z")

</div>

Thanks for your idea! But doesn't work so, changed below:

if (msg.topic == "init"){  
msg1 = {payload:0}  
msg2 = {payload:0}  
msg3 = {payload:0}  
x = 0 // X = Zähler Platten  
}

if (msg.payload == 1 ){x = x + 1}  
if (msg.payload === 0 && x \> 0 ){x = x - 1}

for( t = 1; t \< 4; t++){ // t = Zähler Schleife

if (msg.topic == t && msg.payload == 1 && x \< 3){msg[t] = {payload:1}}  
if (msg.topic == t && msg.payload == 1 && x \> 2){msg[t] = {payload:0}}  
if (msg.topic == t && msg.payload === 0 ) {msg[t] = {payload:0}}

}

msg4 = {payload:x}

return [msg1, msg2, msg3, msg4];

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [31 January 2021 14:18 UTC](https://discourse.nodered.org/t/use-a-variable-in-define-msg-x-instead-of-msg1-msg2-msg3-msgx-in-loop/40108/4 "2021-01-31T14:18:55Z")

</div>

msg[t] ... is creating an array called msg with members t - so msg[1] msg[2] etc...  
so you can just `return [msg];`  
after setting msg[4] = {payload:x};

---

<div class="post-metadata">

### Author: ![Toffy77cc](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@Toffy77cc](https://discourse.nodered.org/u/Toffy77cc)
#### Post date: [31 January 2021 14:55 UTC](https://discourse.nodered.org/t/use-a-variable-in-define-msg-x-instead-of-msg1-msg2-msg3-msgx-in-loop/40108/5 "2021-01-31T14:55:48Z")

</div>

Ok I understand. So I tried like this:

if (msg.topic == "init"){  
msg1 = {payload:0}  
msg2 = {payload:0}  
msg3 = {payload:0}  
x = 0 // X = Zähler Platten  
}

if (msg.payload == 1 ){x = x + 1}  
if (msg.payload === 0 && x \> 0 ){x = x - 1}

for( t = 1; t \< 4; t++){ // t = Zähler Schleife

if (msg.topic == t && msg.payload == 1 && x \< 3){Ausgang[t] = {payload:1}}  
if (msg.topic == t && msg.payload == 1 && x \> 2){Ausgang[t] = {payload:0}}  
if (msg.topic == t && msg.payload === 0 ) {Ausgang[t] = {payload:0}}

}

msg1 = Ausgang[1]  
msg2 = Ausgang[2]  
msg3 = Ausgang[3]  
msg4 = {payload:x}

return [msg1, msg2, msg3, msg4];

But doesn't work anyway ☹

Tank you!

---

<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: [1 April 2021 14:56 UTC](https://discourse.nodered.org/t/use-a-variable-in-define-msg-x-instead-of-msg1-msg2-msg3-msgx-in-loop/40108/6 "2021-04-01T14:56:44Z")

</div>

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