Adding data to a static flow variable Array?

#1

Here is my code snippet for initializing my flow variable:

var a = new Array(65);
flow.set("myArray",a);

In a Function Node I want to add the msg.payload to the myArray flow variable until I fill up the array.
How do I write the correct syntax to add it to the Flow variable?

Something like flow.set.push("myArray",msg.payload); ?????

Thanks...

0 Likes

#2

Firstly it is generally recommented to use
var a = [35];
rather than using new Array. Which is easily misunderstood. For example
new Array(35,60);
may not do what you expect. The simplest way to add a new element is probably

a = flow.get("myArray");
a.push(msg.payload);
flow.set("myArray", a);

If you really want to get it in one statement then
flow.set("myArray", flow.get("myArray").push(msg.payload));

0 Likes

#3

Thanks, works fine!

0 Likes