# How to automatically create global variables using an array

**URL:** <https://discourse.nodered.org/t/how-to-automatically-create-global-variables-using-an-array/65464>\
**Category:** General\
**Tags:** function-node\
**Created:** [22 July 2022 06:27 UTC](https://discourse.nodered.org/t/how-to-automatically-create-global-variables-using-an-array/65464 "2022-07-22T06:27:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaimmor](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaimmor/32/64555_2.png) [@jaimmor](https://discourse.nodered.org/u/jaimmor)\
**Post date:** [22 July 2022 06:27 UTC](https://discourse.nodered.org/t/how-to-automatically-create-global-variables-using-an-array/65464/1 "2022-07-22T06:27:09Z")

</div>

Hello all,

Newb here, trying to learn.

I have a node that will be passing down two string values that I want to assign to two different global variables, var1 and var2  
I am having trouble with my code on how to do that with a for loop

This is what I got, which I know its wrong but I cant wrap my head around how to get this function to do that for me:

var SWSerial = ;  
for (var i in msg.payload){  
global.set(SWSerial[i], msg.payload.serial);  
msg.serial = global.get("SWserial[0]");  
return msg;}

Any guidance is greatly appreciated!!

---

<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:** [22 July 2022 06:43 UTC](https://discourse.nodered.org/t/how-to-automatically-create-global-variables-using-an-array/65464/2 "2022-07-22T06:43:44Z")

</div>

The global path should be a string  
e.g.

```auto
msg.serial = [];
for (var i in msg.payload){
global.set("SWSerial[" + i + "]", msg.payload[i].serial);
msg.serial.push(global.get("SWSerial[" + i + "]"));
}
return msg;

```

example flow

```auto
[{"id":"984f86ea.b649c8","type":"inject","z":"30af2d3e.d94ea2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"serial\":45},{\"serial\":47},{\"serial\":48}]","payloadType":"json","x":90,"y":1320,"wires":[["705d73cd.e1255c"]]},{"id":"705d73cd.e1255c","type":"function","z":"30af2d3e.d94ea2","name":"","func":"msg.serial = [];\nfor (var i in msg.payload){\nglobal.set(\"SWSerial[\" + i + \"]\", msg.payload[i].serial);\nmsg.serial.push(global.get(\"SWSerial[\" + i + \"]\"));\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":1300,"wires":[["143dd92d.782eef"]]},{"id":"143dd92d.782eef","type":"debug","z":"30af2d3e.d94ea2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":460,"y":1320,"wires":[]}]

```

or if separate global vars

```auto
msg.serial = [];
for (var i in msg.payload){
global.set("SWSerial" + i, msg.payload[i].serial);
msg.serial.push(global.get("SWSerial" + i));
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [22 July 2022 07:00 UTC](https://discourse.nodered.org/t/how-to-automatically-create-global-variables-using-an-array/65464/3 "2022-07-22T07:00:07Z")

</div>

what about using a change node - no need for code in this that i can see ?

Craig

---

<div class="post-metadata">

**Author:** ![jaimmor](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaimmor/32/64555_2.png) [@jaimmor](https://discourse.nodered.org/u/jaimmor)\
**Post date:** [23 July 2022 05:22 UTC](https://discourse.nodered.org/t/how-to-automatically-create-global-variables-using-an-array/65464/4 "2022-07-23T05:22:33Z")

</div>

So I took your code and modified it a little to give me what I want, sort of  
I have this:

for (var i in msg.payload) {  
global.set("SWSerial" + i, msg.payload.serial);  
msg.serial = global.get("SWSerial" + i);  
}  
return msg;

My problem now is calling each variableindividually. I try this to test:  
for (var i in msg.payload) {  
global.set("SWSerial" + i, msg.payload.serial);  
}  
msg.serial = global.get("SWSerial[0]");  
return msg;

Now., both variables are the same value. Not sure how moving that outside of the for loop broke that.  
So even if I try [0] or [1], I get only one of the two switches serial number.

I feel like its something so simple now but Ive tried different variations and cant seem to get it.

---

<div class="post-metadata">

**Author:** ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)\
**Post date:** [23 July 2022 08:28 UTC](https://discourse.nodered.org/t/how-to-automatically-create-global-variables-using-an-array/65464/6 "2022-07-23T08:28:08Z")

</div>

You are not following the suggestion from @E1cid

If you use this:

```auto
global.set("SWSerial" + i, msg.payload.serial);

```

you are saving separate variables, like SWSeria1, SWSerial2, etc..

If you want an array, you should follow this other syntax:

```auto
msg.serial = [];
for (var i in msg.payload){
global.set("SWSerial[" + i + "]", msg.payload[i].serial);
msg.serial.push(global.get("SWSerial[" + i + "]"));
}
return msg;

```

---

<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:** [23 July 2022 09:17 UTC](https://discourse.nodered.org/t/how-to-automatically-create-global-variables-using-an-array/65464/7 "2022-07-23T09:17:29Z")

</div>

> [@jaimmor](#):
>
> for (var i in msg.payload) {  
> global.set("SWSerial" + i, msg.payload.serial);  
> }  
> msg.serial = global.get("SWSerial[0]");  
> return msg;

As @fmarzocca says, if you store them as an array you can recall the whole array

```auto
for (var i in msg.payload) {
global.set("SWSerial[" + i +"]", msg.payload[i].serial);
}
msg.serial = global.get("SWSerial");
return msg;

// or using forEach()
msg.payload.forEach((obj, index) => {
    global.set("SWSerial[" + index + "]", obj.serial);
})
msg.serial = global.get("SWSerial");
return msg;

```

Also you get the same serial in both because you save the same value `msg.payload.serial`. I am guessing msg.payload should be an array as i have shown in my examples.

If this is not correct please supply an example of msg.payload and exactly how you wish SWSerial global to be stored.

---

<div class="post-metadata">

**Author:** ![jaimmor](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaimmor/32/64555_2.png) [@jaimmor](https://discourse.nodered.org/u/jaimmor)\
**Post date:** [29 July 2022 18:00 UTC](https://discourse.nodered.org/t/how-to-automatically-create-global-variables-using-an-array/65464/8 "2022-07-29T18:00:56Z")

</div>

Ok so now that I think about it more, I don't think it matters how I accomplish this, meaning, through an array or through separate variables, my goal is to be able to call one of my switches serial, whether thats using an array (SWSerial[0]), or completely different variables (SWSerial1, SWSerial2, etc.)  
Ultimately, I imagine I would be able to use the array to create the distinct variables if I wanted to.  
Hope that makes sense.

I tried:

for (var i in msg.payload) {  
global.set("SWSerial[" + i +"]", msg.payload[i].serial);  
}  
msg.serial = global.get("SWSerial");  
return msg;

And that gave me:  
Error: Invalid property expression: unexpected n at position 9

To add more color, this function is receiving two objects, both with identical attributes, different values. One of those being msg.payload.serial.

I hope that helps, and I appreciate you guys taking the time on this to help me figure it out.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [30 July 2022 05:02 UTC](https://discourse.nodered.org/t/how-to-automatically-create-global-variables-using-an-array/65464/9 "2022-07-30T05:02:07Z")

</div>

You need to put some debugging nodes (and name them) on your msg flow and post up a picture of the flow and the outputs from the Debug nodes so we can see exactyl what the messages look like please

Craig

---

<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:** [28 September 2022 05:02 UTC](https://discourse.nodered.org/t/how-to-automatically-create-global-variables-using-an-array/65464/10 "2022-09-28T05:02:08Z")

</div>

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