Extract Array Elements within Script

Hi Team

Is it possible to extract individual array elements withing script? I have a Data Action which outputs an array and I am executing the DA as script page load action and mapping the DA output to string (List Type) variable.

DA Output is like (Please note that this output is variable and based on search there can be more or less elements in the array )

"List": [
"Item 1",
"Item 2",
"Item 3",
"Item 4",
.
.
.
]

What I get in Script is below as single line/string.

Item 1, Item 2, Item 3, Item 4 , . . .

I would like to get the individual elements like Item 1 or Item 2 or Item 3 so on. Given that my script variable type is a List, I assume there is a way to extract individual list items but I am unable to figure it out hence posting for any recommendations

Hello,

I'm not a frequent user of Script but I have just tried to create one that leverages a List of string in a script (coming from a Data Action).

My Data Action is providing an array of string as output parameter ("mylist" in the following Output Contract):

{
  "type": "object",
  "properties": {
    "mylist": {
      "type": "array",
      "items": {
        "title": "Item 1",
        "type": "string"
      }
    }
  },
  "additionalProperties": true
}

In my Script, I have created a String List variable - let's say RetrievedList - used as the output variable for my Data Action (for the "mylist" parameter).

I can access its values via a Dynamic String variable - e.g.: {{RetrievedList}}[1] for first element
Apparently index starts on 1.
I could also get the length via a Dynamic Number variable - e.g.: length({{RetrievedList}})
I haven't investigated much - but my length (before assigning result of data action) is 1 when the String List variable is initialized. I haven't tried to test the value of first element in this case (empty string, not set, ...).

In order to push element to the array (if you need this outside of the Data Action result), you have a new Script action that needs to be enabled in Script properties. See List - Push section.

Regards,

1 Like

Hi Jerome

Great, Dynamic String variable did the trick. thanks for your response.

a couple more questions if you dont mind.

  1. Is there a way to automate above (i.e. retrieving the elements) given that we have the length of the "RetrievedList" - for example in Architect we can create a loop and update index in each iteration to retreive the elements. Can something like this be done within the script?
  2. Is there a reference document that lists different functions supported within scripts - similar to the "length" function you mentioned.

Thanks

Hello,

As I mentioned, I don't use Scripts often.
"scripting" in Script is limited so it will depend on what you are trying to implement.

What I know is that list variables provide list support in dropdowns.
You can also define custom actions, which can leverage if/then tests. See Define a custom action for instructions.
Regarding variables and functions, this is what is available:

Regards,

Hi Jerome

appreciate any assistance you are able to provide. One of the things I am trying to do is to list all the elements from {{RetrievedList}} as dropdown list but I can only do this by manully assigning individual items {{RetrievedList}}[1]

i.e.
RetrievedList: Item1,Item2,Item3,....
DynamicItem1: {{RetrievedList}}[1]
DynamicItem2: {{RetrievedList}}[2]
.
.
.

Then in the Dropdown I add {{DynamicItem1}} and so on

image

and it displayes the item

image

but I would like to create the dropdown list automatically from {{RetrievedList}}. Is it possible?

Other thing I noted that some string functions work and some don't. For example indexOf("ABCD", "B") returns 1 which is expected but indexOf(["a","b","c"], "b") does not return anything. Same goes for getIndexValue(["a","b","c"], 1), returns nothing. I am using these expressions within Dynamic String as below. any ideas why?

image

Thanks

1 Like

Hello,

Regarding the Dropdown component:
When you create/select your component, In "Options", on the left of the "Edit List" button, you can select "Variable" as the source. You can then pick {{RetrievedList}} as the variable. It will automatically display all elements of the list in the dropdown component.

Regarding your functions. I am not familiar with Script that much but running a quick test, it seems that you need to use a variable for your array.
I mean that assuming that RetrievedList = ["a","b","c"], then indexOf({{RetrievedList}}, "b") will work. Same for getIndexValue({{RetrievedList}}, 1).

Regards,

Hi Jerome,

Excellent. appreciate your assistance.

Cheers,

1 Like

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