Set value inside an app variable array

Hi there,

I have a quick question that I feel should be easy but somehow isn’t working for me.

I have an App Variable called “bank”, which is type Object with two properties: id (text), shares (array of numbers).

I also have a Page Variable called “amount” of type Number with the value 2, and another Page Variable called “property” of type Number with the value 1.

In a previous page I set the values in the ‘shares’ array to be [9,9,9,9,9,9,9,9,9].

What I would like to do now is set a specific array item (using “property” as the index) to the “amount” value via the Set app variable flow function.

In the ‘variable name’ I select “bank”, then “shares” and in the UI of composer the variable name is populated to read ‘bank.shares’.

Then I am lost on what to put in the ‘assigned value’ box below.

I thought of putting a formula in ‘variable name’ input field but the only two options I have is App Variable and Output of another node.

To resume: how do I execute the following operation using the Inputs provided in the Composer UI for the ‘Set app variable’ flow function

appVars.bank[property] = amount

With the result that the App Variable bank.shares would be [2,9,9,9,9,9,9,9]

Thanks!

Hi there!

We would like to have a more elegant solution for this, but for now you can use MAP to set the array item like this:

MAP<item, index>(array, IF(index == 0,"new value" ,item) )

Assuming ‘array’ is [1,2,3] this would result in ["new value",2,3]

Hope this helps!

Thanks - that does help as the formula side of it.

But unfortunately my problem still stands: the ‘Set app variable’ flow function does not allow Formula as an input, just ‘app variable’ or ‘output of previous node’. So how would I use the formula you propose?

I thought maybe inside a JS module, but my understanding from the documentation is that the JS module environment is isolated - meaning even if I pass my “bank” object and manipulate the array inside JS, those changes will not take place on the actual AppGyver App Variable.

@Ender_Wilde

Hi, you should use the formula in the “Assigned value” field, which is the value to be assigned to the chosen app variable. The “Variable name” only allows you to choose a variable / output of previous node, which is by design :slight_smile:

Ah ok gotcha.

And the MAP formula returns an array, so in effect it would be ‘swapping in’ the bank.shares array for a new one resulting from the MAP operation?

I have a related question about choosing variable names dynamically but will create a new topic for that.

Thanks!

@Ender_Wilde Yes, indeed MAP returns the transformed array, which is then used as a new value to be saved into the variable. (semantically speaking there is no “update variable” so this is how updates are handled in general :slight_smile: )