We have already taken a look at different variable types, but there is a special type that we should explore a bit more—the array. Any variable type can be made into an array. An array is a container that holds multiple values of the same variable type. For example, an array of the String variable type may contain values such as "Mum", "Dad", "Sister", and "Brother", whereas an array of VC:VirtualMachine may contain multiple VMs.
In JavaScript, you can use several methods to start, define, and initialize an array. The following are some of these methods:
var family = new Array("Mum", "Dad", "Sister", "Brother");
var family=["Mum", "Dad", "Sister", "Brother"];
var family = new Array();
family.push("Aunt");
Typically, we access the content of an array via JavaScript. To access an array, we use square brackets ([ ]
) and write in it the number of the elements that you would like to access. Please note that the content count of an array starts from 0 (zero). For example, family[2]
will return "Sister"
, and family[0]
will return "Mum"
.
We will work on an example in a second.
Arrays are used a lot in Orchestrator, and you need to know how one can define them in the workflow as a parameter.
We will now create an array in the JavaScript example workflow that we created in the last section. We will first define an array and then fill it with content, as follows:
Mum
, Dad
, Sister
, and Brother
.We created an array in the workflow and filled it with values. We will now use JavaScript to read it:
arrayOut = family [myFirstValue];
Now, we will add a value to the family array:
family = family.push(myFirstValue);
So, now that we have learned about arrays, we can proceed to use an array in our workflow.
We are doing quite well in regards to the improvements to our workflow, but we still have some issues. For instance, a user can still enter 1.5 CPUs, which isn't going to work. So, we will offer the user only a list of values that they can choose from by performing the following steps:
1
, 2
, 3
, and 4
values and click on Accept.