JavaScript complex variables

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.

Getting ready

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.

How to do it...

We will look into several different JavaScript complex variable type pieces.

Arrays

Arrays are pretty useful for storing multiple elements of the same type:

  1. To define a new empty array in JavaScript, use either one of the following codes:
    • var family = new Array();
    • var myArray = [];
  2. You can define a new filled array by using either of the following examples:
    • var family = new Array("Father","Mother","Daughter","Son");
    • var myArray = ["Father","Mother","Daughter","Son"];
  3. While accessing the element (numbering starts at zero), System.log(family[2]); will show Daughter.
  4. You can add element(s) with push. You can add multiple ones using a ",":
    • family.push("baby");
  5. The amount of elements in an array can be found with length:
    • System.log(family.length);
  6. As seen in the recipe Scripting with loops inChapter 5, Visual Programming, you can loop through an array with a 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.

Properties

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.

  1. Define a new property using the following code:
          var relatives= new Properties(); 
    

    Tip

    Note that even if you define a property in Orchestrator (attributes or out-parameters), you still need to initialize it (see step 1) before you can fill it.

  2. Add a key (Uncle) and a value (David) to the property:
         relatives.put("Uncle","David"); 
    

    Tip

    Please note that all Property and Object keys are case sensitive.

  3. You read elements by referring to its key (Uncle). The value will be shown as follows:
         System.log(relatives.get("Uncle")); 
    
  4. Show all keys in a property (as an array):
         System.log(relatives.keys); 
    
  5. Loop through properties:
         for each (key in relatives.keys) { 
            System.log(key+" : "+ relatives.get(key)); 
         } 
    
  6. Remove an element from a property:
         relatives.remove("Aunt"); 
    

Objects

Objects are a bit like properties, although not as well defined and user-friendly.

  1. Declare a new object:
          var pets = new  Object(); 
    
  2. Create a property named Dog with the value Fido for the object. Use either one of the following:
    • pets["Dog"]="Fido";
    • pets.Dog="Fido";
  3. Read out an object. It will return Fido as a string. Use either one of the following:
    • System.log(pets["Dog"]);
    • System.log(pets.Dog);
  4. To create an array inside an object, use the following form:
          var pet.mice=["mouse1"," mouse2"," mouse3"," mouse4"] 
    
  5. To read this array, use the following:
          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.

How it works...

Complex variables are used quite often in Orchestrator, especially when we are using plugins such as PowerShell.

Array methods

The following table shows all the methods that are associated with arrays:

Function                           

Explanation

array3=array1.concat (array2)

Joins two arrays and returns the new one.

string=array.join (separator)

Makes a string out of the array using the separator between elements.

any=array.pop()

Removes the last element and returns it.

number=array.push(Any)

Adds a new element to the end of the array and returns a new length.

array.reverse()

Reverses the order of the elements in the array.

any=array.shift()

Removes the first element and displays it.

newArray=array.slice(n,m)

Slices an array from n to m out of an array.

array.sort()

Sorts an array alphanumerically.

array.splice(n,m,any)

Inserts (and/or removes) elements.

number=array.unshift(any)

Inserts an element at the beginning of the array and returns a new length.

number=array.indexOf(any)

Returns the index of the element you are looking for.

Properties within properties

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)); 
} 

Array of properties

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.

See also

See the example workflow 06.01 JavaScript complex variables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset