Array Type Flow Variable

Kirby R
Kilo Sage

HI,

 

I want to create an array type variable so I can iterate these later on in a for each.  But it seems there's no avaiable array type in flow designer.

 

KirbyR_0-1673502010346.png

Any workaround for this? Thanks.

7 REPLIES 7

louison
Tera Contributor

Hello, I had the same problem today and this is the "workaround" that I ended up going for.

 

1. Create a flow variable with type JSON.

 

2. Initialize the value of that object with a property that will hold your array. this requires scripting to set the value:

return { content: [] };​

(see below screenshot: your initializing "set flow variables" step would look like that)

louison_0-1748594763537.png

 

3. whenever you need to add any value to your array, assign a new value to your JSON flow variable where the inner array is enhanced according to your needs. this also requires a small amount of scripting:

var myArray = fd_data.flow_var.makeshift_array.content;
myArray.push("a new value in my collection");
// don't forget, your flow variable is actually a javascript OBJECT wrapping an array
// you need to return the whole object to properly update the value
return { content: myArray }; ​
	

(see below screenshot: your updating "set flow variables" step would look like that)

louison_1-1748595185898.png

 

 

The string-based solution proposed by David works too but I found that the JSON-based solution has a few updsides (in no specific order):

 

- no need to worry about separator, no need to make sure the separator doesn't appear in the values you concatenate together (which would result in wrong "parsing", returning more elements that what was actually added), no need to parse the collection every time a computation must be done on any of its element

 

- slightly easier and more explicit/readable access to a given element of the collection:

// JSON-based solution
var jElement2 = fd_data.flow_var.makeshift_array.content[2];
// String-based solution; assuming the separator used is ","
var sElement2 = fd_data.flow_var.sep_string.split(",")[2]​
	

 

- storing different kinds of values: numbers, strings, objects, ... for instance you could write a script of the following sort in your updating "set flow variables" step:

var myArr = fd_data.flow_var.makeshift_array.content;
var myNewComplexValue = {
    descr: String(fd_data._3__for_each.item.short_description),
    prio: Number(fd_data._3__for_each.item.priority)
};
myArr.push(myNewComplexValue);
return { content: myArr }​
	

 

- having useful array methods at the ready

// after collecting a few values, I now want to only keep three of them with the most urgent prio
var myArr = fd_data.flow_var.makeshift_array.content;
// sort the elements in the array according to their prio field (in ascending numeric order)
myArr.sort(function(objWithPrio1, objWithPrio2) { return objWithPrio1.prio - objWithPrio2.prio; });
// only keep 3 of the elements
var top3Arr = myArr.slice(0, 3);

// after that: do what you want with the top3 most urgent objects!

​
	

 

This solution is not perfect though: you always have to keep in mind that your flow variable is not an array, but rather an array wrapped in an object; and whenever you need to access or update your array you have to manually wrap/unwrap.

Probably the best workaround.

@louison -

 

Nice thought process must say!