In this recipe, we will have a look at JavaScript complex variables, such as arrays, objects, and properties. All these are needed to deal with plugin return codes such as from REST calls.
We need a new workflow and a scriptable task inside it to try it out.
The example workflow 06.01 JavaScript complex variables
contains all the examples that follow.
We will look into several different JavaScript complex variable type pieces.
Arrays are pretty useful for storing multiple elements of the same type:
var family = new Array();
var myArray = [];
var family = new Array("Father","Mother","Daughter","Son");
var myArray = ["Father","Mother","Daughter","Son"];
System.log(family[2]);
will show Daughter
.push
. You can add multiple ones using a ",
":family.push("baby");
length
:System.log(family.length);
for each
loop:for each (element in family) { system.log(element) }
These are the basic functions; there are additional ones discussed in the How it works... section of this recipe.
The properties
variable type can be extremely useful to transport complex content. A property can be seen as a hash table, which means that you can assign multiple pairs of keys with their values.
var relatives= new Properties();
Uncle
) and a value (David
) to the property:relatives.put("Uncle","David");
Uncle
). The value will be shown as follows:System.log(relatives.get("Uncle"));
System.log(relatives.keys);
for each (key in relatives.keys) { System.log(key+" : "+ relatives.get(key)); }
relatives.remove("Aunt");
Objects are a bit like properties, although not as well defined and user-friendly.
var pets = new Object();
Dog
with the value Fido
for the object. Use either one of the following:pets["Dog"]="Fido";
pets.Dog="Fido";
Fido
as a string. Use either one of the following:System.log(pets["Dog"]);
System.log(pets.Dog);
var pet.mice=["mouse1"," mouse2"," mouse3"," mouse4"]
System.log(pets.mice.length); System.log(pets.mice[2]);
A good usage for objects is to use them together to form JSON objects.
We will take look at JSON in the recipe Working with JSON in this chapter.
Complex variables are used quite often in Orchestrator, especially when we are using plugins such as PowerShell.
The following table shows all the methods that are associated with arrays:
Function |
Explanation |
|
Joins two arrays and returns the new one. |
|
Makes a string out of the array using the separator between elements. |
|
Removes the last element and returns it. |
|
Adds a new element to the end of the array and returns a new length. |
|
Reverses the order of the elements in the array. |
|
Removes the first element and displays it. |
|
Slices an array from |
|
Sorts an array alphanumerically. |
|
Inserts (and/or removes) elements. |
|
Inserts an element at the beginning of the array and returns a new length. |
|
Returns the index of the element you are looking for. |
Sometimes, you have properties inside properties. This is harder than it sounds. Just have a look at the example here:
var mailProperties= new Properties(); mailProperties.put("mailTo","[email protected]"); mailProperties.put("mailCC","[email protected]"); mailProperties.put("subject","Test email"); var mailReplacements= new Properties(); mailReplacements.put("vm.name","myVM"); mailReplacements.put("vm.ip","192.168.220.10"); mailReplacements.put("vm.mac"," 0A:0B:0C:0D:0E:0F"); mailProperties.put("mailReplacements",mailReplacements);
To read, use this example:
mailTo=mailProperties.get("mailTo"); keys=mailProperties.keys if (keys.indexOf("mailCC")>=0){ mailCC=mailProperties.get("mailCC"); } mailSubject=mailProperties.get("subject"); mailReplacements=mailProperties.get("mailReplacements"); for each (key in mailReplacements.keys){ System.log(key+" : "+mailReplacements.get(key)); }
An array of properties can be even more useful, as JavaScript doesn't really do multidimensional arrays. An array of properties is more or less a two-dimensional array. To create an array of properties, use the following steps:
Var myArray = new Array(); var myProp = new Properties() ; myProp.put("myKey","Key Value") ; myArray.push(myProp);
Here's an example of an array of properties:
Var mails = new Array(); var mail = new Properties() ; mail.put("subject","Test Email 1") ; mails.push(mail); var mail = new Properties() ; mail.put("subject","Test Email 2") ; mails.push(mail);
We will use properties in the recipe Working with mail in Chapter 9, Essential Plugins.