Consider this setup where we have two arrays. Let’s see how to extract specific data from these arrays using the Javascript node.

In the Javascript node, configure the Inputs and Outputs tab in the Editor panel:


Code


Here is the script in the Main section of the Script tab :

output_1 = input_2
output_2 = input_1["bbb:x"]
output_3 = input_2_ids[0]
output_4 = input_1_ids[1]
output_5 = input_2_propertiesName[2]


An example of arrays being input in a javascript node


Detail


Let’s detail line by line how we configured our outputs using javascript :

  • output_1 = input_2 : In output_1 we’ll output whatever we plug into input_2 as is, without modification.
  • output_2 = input_1["bbb:x"] : In output_2 we’ll look into our input_1 for an ID named bbb and for that ID we’ll extract whatever value is held by the property named x.
    Note : If the ID bbb does not exist in the input, or if it does not have a property named x then the output will be empty.
  • output_3 = input_2_ids[0] : In output_3 we’ll look into our input_2 and extract the ID for the index 0.
  • output_4 = input_1_ids[1] : In output_4 we’ll look into our input_1 and extract the ID for the index 1.
  • output_5 = input_2_propertiesName[2] : In output_5 we’ll look into our input_2 and extract the name of the third property.
    Note : Properties, like indexes, are numbered starting from 0. Hence, propertiesName[2] points to property number 3, and not number 2.

Passing arrays as strings


It is possible to input an array from Kinetic’s graph editor into a string input on the Javascript node.

In this case, the array will exist in the Javascript code as a string and can be parsed using the string methods available in javascript.

One useful technique consists in creating an internal array variable in your script and storing every line of your source array as one item in your internal array.

let internal_array = source_array.split(";");

You can scan this internal array to perform various operations by using a for loop :

For example, to check the presence of “TARGET TAG” in your source array or if an item has a Lifetime value of 314 :

for (let i = 0; i < internal_array.length; i++) {
    let mystr = internal_array[i];
        if (mystr.includes("TARGET TAG") {
            //Do something
        }
        if (mystr.includes("Lifetime@314") {
            //Do something else
        }
    }

Refer to the Array page of this documentation for more information on the syntax used by arrays to store data.

Need more help with this?
Don’t hesitate to contact us here.

Thanks for your feedback.