# How to build a flow variable array in a function

**URL:** <https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585>\
**Category:** General\
**Created:** [23 July 2020 07:04 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585 "2020-07-23T07:04:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![zazas321](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zazas321](https://discourse.nodered.org/u/zazas321)\
**Post date:** [23 July 2020 07:04 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/1 "2020-07-23T07:04:16Z")

</div>

Hello. I am a little bit stuck trying to build a flow variable array inside a function. I need this variable to be flow or global so I can use it in other functions:

The function look like this

```auto
//add the values to the msg so they can be debugged and used later

quantity = msg.payload
var device_id = msg.topic.split("/");// split the whole topic to devicex ( untill /)
msg.payload = device_id[0];
msg.topic=`UPDATE pack_to_light SET Quantity = '${quantity}' WHERE Device = '${device_id[0]}'`

flow.set("individual_items",msg.payload);
node.warn(flow.individual_items)

return msg;

```

This function will be triggered multiple times (may vary). For example, if this function is gets triggered 3 times, every time its triggered I set a new payload value to it as such:

```auto
msg.payload = device_id[0];

```

I need to set this msg.payload to flow variable named "individual\_devices". I have attempted to set "individual\_devices" to msg.payload but I cant figure out how do I build array out of it since I need the result to be something like that ( If function is triggered 3 times)

flow.individual\_devices[0] = msg.payload1  
flow.individual\_devices[1] = msg.payload2  
flow.individual\_devices[2] = msg.payload3

In short, how can I keep track how many times the function did execute so I can create a variable length array for " individual\_devices"

---

<div class="post-metadata">

**Author:** ![zazas321](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zazas321](https://discourse.nodered.org/u/zazas321)\
**Post date:** [23 July 2020 07:22 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/2 "2020-07-23T07:22:39Z")

</div>

Also, can I read flow type variable as such?:  
flow.Individual\_devices  
or I must use flow.get function?  
flow.get('Individual\_devices' )

---

<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:** [23 July 2020 07:46 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/3 "2020-07-23T07:46:42Z")

</div>

Yes, use flow.get(... And flow.set(..

---

<div class="post-metadata">

**Author:** ![zazas321](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zazas321](https://discourse.nodered.org/u/zazas321)\
**Post date:** [23 July 2020 07:51 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/4 "2020-07-23T07:51:34Z")

</div>

Thanks sir. I have managed to make slight progress:

```auto
//add the values to the msg so they can be debugged and used later
flow.set('internal_counter',0)
node.warn(flow.get('internal_counter'))
quantity = msg.payload
var device_id = msg.topic.split("/");// split the whole topic to devicex ( untill /)
msg.payload = device_id[0];
msg.topic=`UPDATE pack_to_light SET Quantity = '${quantity}' WHERE Device = '${device_id[0]}'`

//flow.set("individual_items",msg.payload);
//node.warn(flow.get('individual_items'))
flow.set('internal_counter',flow.get('internal_counter')+1)
node.warn(flow.get('internal_counter'))
return msg;

```

I have implemented internal counter inside function , the problem is that I have initially set this counter to 0, and then increment it, but when the function gets called another time, it sets it back to 0. Is it possible to declare internal\_counter to 0 just once?

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [23 July 2020 08:04 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/5 "2020-07-23T08:04:58Z")

</div>

You could use something like `var internal_counter = flow.get("internal_counter") || 0;` and than use this var in your set statement instead of another get statement.  
This way if the counter is not set yet it will be zero.

---

<div class="post-metadata">

**Author:** ![zazas321](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zazas321](https://discourse.nodered.org/u/zazas321)\
**Post date:** [23 July 2020 08:16 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/6 "2020-07-23T08:16:52Z")

</div>

Could you tell me what exactly does || 0 does?

My current function;

```auto
var internal_counter = flow.get("internal_counter")||0;
flow.set("individual_items[internal_counter]",msg.payload);
node.warn(flow.get('individual_items[internal_counter]'))
flow.set('internal_counter',internal_counter+1)
node.warn(flow.get('internal_counter'))
return msg;

```

So now, the internal\_counter is not set back to 0 evertyime the function is called but now, it just keeps incrementing the inernal\_counter even when I restart the flow it still retains the last counter value. How would I make so that everytime the flow is deployed or restarted, the counter automatically goes to 0.

Also, do you spot any obvious errors in my function since I cant seem to set flow variable individual\_items[internal\_counter] because I am getting error:

```auto
23/07/2020, 11:11:14node: Update DB quantity when reached 0, set device names to global
function : (error)
"Error: Invalid property expression: unexpected i at position 17"
23/07/2020, 11:11:14node: Update DB quantity when reached 0, set device names to global
function : (error)
"Error: Invalid property expression: unexpected i at position 17"

```

I dont understand what it means by unexpected i at since I dont have any i in my function

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [23 July 2020 08:29 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/7 "2020-07-23T08:29:07Z")

</div>

> [@zazas321](#):
>
> `flow.set("individual_items[internal_counter]",msg.payload);`

should be

```auto
flow.set("individual_items['internal_counter']",msg.payload);

```

see the single quotations within the square bracket?

> [@zazas321](#):
>
> Could you tell me what exactly does || 0 does?

It initializes the variable to 0 if the flow var you are trying to get is undefined. To get it back to 0 you should set the flow var purposely to 0 once the counter is done.  
You can initialize it to zero on nodered start by using a inject node set to fire once connected to a change node that sets the flow var to zero.

---

<div class="post-metadata">

**Author:** ![zazas321](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zazas321](https://discourse.nodered.org/u/zazas321)\
**Post date:** [23 July 2020 08:57 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/8 "2020-07-23T08:57:02Z")

</div>

Thanks for your help.  
Do we use single quotation marks to say that this is a variable?  
double quotation marks would mean that this is a string?

```auto
var internal_counter = flow.get("internal_counter")||0;
flow.set("individual_items['internal_counter']",msg.payload);

node.warn(flow.get('individual_items["internal_counter"]'))

```

my node.warn does not execute properly - I think there is some problem with quotation marks?

WHat is the diference between

```auto
node.warn(flow.get('individual_items["internal_counter"]'))
and
node.warn(flow.get('individual_items['internal_counter']'))

```

(See the quotation marks near the "internal\_counter")

The first example prints "undefined"

The second example wont even deploy becaue of:

 ![2020-07-23-115630_1920x1080_scrot](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/f/cf5138d28fb06109dde4f0da1bdb5fa1611dd062.png)

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [23 July 2020 09:10 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/9 "2020-07-23T09:10:45Z")

</div>

I think I got wrong what you wanted to do. What I gave you was the syntax to access a property of the individual\_items **object** called _internal\_counter_, but its an **array** right(?) and you want to access an index in that array by the local `internal_counter` var?  
Ok my fault 🙈 sorry 🙇‍♂️  
To use a local var as an array index in a get or set statement use string concatenation:

```javascript
flow.set("individual_items[" + internal_counter + "]",msg.payload);

```

or you could also use a template string if you feel fancy:

```javascript
flow.set(`individual_items[${internal_counter}]`,msg.payload);

```

_Edit_  
Concerning the `""` and `''` in general in javascript if you want to use quotations within a string there is multiple ways. As javascript supports both as string annotations you can either use the opposite with what you annotated the string within the string. So either:

```javascript
var test = 'this is "a test"';

```

or

```javascript
var test = "this is 'a test'";

```

or you can escape the quotations with a backslash if you want to use the same:

```javascript
var test = "this is \"a test\"";

```

---

<div class="post-metadata">

**Author:** ![zazas321](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zazas321](https://discourse.nodered.org/u/zazas321)\
**Post date:** [23 July 2020 09:33 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/10 "2020-07-23T09:33:06Z")

</div>

Yes you are correct.

I am not able to node.warn print what individual\_items are assigned to:  
Trying many different ways but get many different errors

```auto
node.warn(flow.get('individual_items[internal_counter]'))
node.warn(flow.get('individual_items['internal_counter']'))
node.warn(flow.get('individual_items["internal_counter"]'))
node.warn(flow.get('individual_items["+internal_counter+"]'))
node.warn(flow.get("individual_items["+internal_counter+"]"))

```

---

<div class="post-metadata">

**Author:** ![zazas321](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zazas321](https://discourse.nodered.org/u/zazas321)\
**Post date:** [23 July 2020 09:38 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/11 "2020-07-23T09:38:58Z")

</div>

The closes I got is using this function:

```auto
node.warn(flow.get("individual_items["+internal_counter+"]"))

```

 ![2020-07-23-123752_1920x1080_scrot](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1e86f6ee13575dace665e247c9ca135b3a220f3d.png)  
As you can see from the debug menu, it returns first and second letter of the individual\_items instead of returning first and second element of array

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [23 July 2020 09:45 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/12 "2020-07-23T09:45:12Z")

</div>

> [@zazas321](#):
>
> `node.warn(flow.get("individual_items["+internal_counter+"]"))`

This one should work if:

- a: individual\_items is an array that is defined in flow.context
- b: internal\_counter is a valid index of individual\_items

Have a look at this simple example and what you see in the debug tab when you inject:

 ![Screenshot 2020-07-23 at 11.39.37](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cb1bc504036181e3ef8364574c70c19dd73ec4fa.png)

```auto
[{"id":"4678cb7c.22a7d4","type":"function","z":"7782f553.5f6a14","name":"","func":"flow.set(\"test\",[0,0,0,0,0]);\nnode.warn(flow.get(\"test\"));\nvar num = 3;\nflow.set(`test[${num}]`,16);\nnode.warn(flow.get(\"test\"));\nnode.warn(flow.get(\"test[\" + num + \"]\"));\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":1340,"wires":[[]]},{"id":"b1d94137.92985","type":"inject","z":"7782f553.5f6a14","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":260,"y":1340,"wires":[["4678cb7c.22a7d4"]]}]

```

This is in the function node:

```javascript
flow.set("test",[0,0,0,0,0]);
node.warn(flow.get("test"));
var num = 3;
flow.set(`test[${num}]`,16);
node.warn(flow.get("test"));
node.warn(flow.get("test[" + num + "]"));
return null;

```

and this is the output as expected:

 ![Screenshot 2020-07-23 at 11.43.02](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/34563b5ea6050394fb9ddc87a305fb4a9e673b0f.png)

Have you checked that you properly set your array flow variable `individual_items`?  
What do you see in the context browser?  
Like for my example above:

 ![Screenshot 2020-07-23 at 11.44.56](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/2259b591dc762231cdffe3e87c88972242643dc3.png)

---

<div class="post-metadata">

**Author:** ![zazas321](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zazas321](https://discourse.nodered.org/u/zazas321)\
**Post date:** [23 July 2020 09:48 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/13 "2020-07-23T09:48:24Z")

</div>

I have realized that I have not even initialized my individual\_items array. Can I do that If I do not know the length of it?

I am just using flow.set to set it to msg.payload but I havent even declared the individual\_items

---

<div class="post-metadata">

**Author:** ![zazas321](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zazas321](https://discourse.nodered.org/u/zazas321)\
**Post date:** [23 July 2020 09:54 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/14 "2020-07-23T09:54:36Z")

</div>

```auto
// MISSING INDIVIDUAL_ITEMS INITIAL DECLARATION
var internal_counter = flow.get("internal_counter")||0;

flow.set("individual_items["+internal_counter+"]",msg.payload);
node.warn(flow.get("individual_items["+internal_counter+"]"))
flow.set('internal_counter',internal_counter+1)
node.warn(flow.get('internal_counter'))

return msg;

```

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [23 July 2020 10:01 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/15 "2020-07-23T10:01:58Z")

</div>

You can do the same trick as `|| 0` but use `|| []` but this will give you an empty array which doesnt change that `internal_counter` has to be a valid index within that array.  
It looks to me like you should look at array methods and how javascript arrays actually work.  
You can only set indexes in arrays that exist. To add to an array you will have to look at methods like array.push() and the same goes for removing array elements. You will have to use methods like slice() or pop(). Please do read this whole section about js array methods:

> **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#)**
>
> The JavaScript Array class is a global object that is used in the construction of arrays; which are high-level, list-like objects.

Before you dont understand the basics of this you will just run into one Roadblock after another and other people and me will have to feed you information bite by bite which will get very tedious for you and everybody else.

---

<div class="post-metadata">

**Author:** ![zazas321](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zazas321](https://discourse.nodered.org/u/zazas321)\
**Post date:** [23 July 2020 10:23 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/16 "2020-07-23T10:23:36Z")

</div>

I have read the article -thanks.

However, it still not clear for me how do I set the flow variable to be array type.

Normally, I would just use:  
` ` `var items_array = []` ` `  
That would create a variable items\_array of unknown size

The hard part for me is understanding the node-red sytax.

I have also read this article:  
[https://nodered.org/docs/user-guide/writing-functions](https://nodered.org/docs/user-guide/writing-functions)  
And could not find any hints on how to create a flow that would be array type.

You have mentioned using `|| [] ` and that sort of makes sense to me except that I am not sure how to use it? did you mean something like:

```auto
var items_array = flow.get("internal_counter") || []

```

Now my items\_array will be a flow type variable array?

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [23 July 2020 10:29 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/17 "2020-07-23T10:29:10Z")

</div>

> [@zazas321](#):
>
> ```auto
> var items_array = flow.get("internal_counter") || []
> 
> ```
> 
> Now my items\_array will be a flow type variable array?

As I explained above this will initialize `var items_array` to be an empty array should `flow.internal_counter` be undefined / not initialized.  
For `flow.internal_counter` to be an array you would than have to do a `flow.set("internal_counter", items_array);` at some point after the get statement as the `|| []` doesnt change the actual `flow.internal_counter` but just set `var items_array` to `[]` if the prior is undefined.

---

<div class="post-metadata">

**Author:** ![zazas321](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zazas321](https://discourse.nodered.org/u/zazas321)\
**Post date:** [23 July 2020 10:37 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/18 "2020-07-23T10:37:21Z")

</div>

Okay, I just did  
` ` `var items_array = [] ; flow.set("individual_items" ,items_array);` ` `  
Now it lets me set the individual elements of a individual\_items because it is set as an array.

Thank you very much. Got there in the end

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [23 July 2020 10:39 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/19 "2020-07-23T10:39:14Z")

</div>

Just make sure to only do this once / when the flow.var is undefined or you will reset the array to be empty every time you run the function.

---

<div class="post-metadata">

**Author:** ![zazas321](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zazas321](https://discourse.nodered.org/u/zazas321)\
**Post date:** [23 July 2020 10:44 UTC](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585/20 "2020-07-23T10:44:41Z")

</div>

```auto
var items_array =var items_array || []

```

Would this not be correct?

Since I am not holding any information in the items\_array, I am just using this to set the flow variable as array

```auto
var items_array = []
//var items_array = flow.get("individual_items")|| []
flow.set("individual_items",items_array);

flow.set("individual_items["+internal_counter+"]",msg.payload);
node.warn(flow.get("individual_items["+internal_counter+"]"))
flow.set('internal_counter',internal_counter+1)
node.warn(flow.get('internal_counter'))

```

[Next page](https://discourse.nodered.org/t/how-to-build-a-flow-variable-array-in-a-function/30585.md?page=2)
