Forum Discussion

easy-soft's avatar
easy-soft
Occasional Contributor
5 years ago

Javascript: how to create a variant array of strings

From my TC project I want to pass an array of strings to my Delphi application from a JavaScript unit. If I have understood the matter, I need to pass this array as variant.

To create the variant array I try to use the CreateVariantArray function using the following code:

 

var varArray = BuiltIn.CreateVariantArray(0,1);
varArray[0] = "value 1";
varArray[1] = "value 2";

 

Unfortunately this only creates the array but the elements remain empty: debugging inside TC shows the Locals panel with the array but it has two empty entries and inside the receiving function in my Delphi application the array also has two elements which are both empty.

 

Searching this board and the internet did not reveal any hints. Many thanks in advance for any help.

  • Okay, I can answer my own questions. Actually the solution is given in the documentation for CreateVariantArray: 

     

     

    function ConvertArray(Array)
    {
      // Uses the Dictionary object to convert an array
      var objDict = getActiveXObject("Scripting.Dictionary");
      objDict.RemoveAll();
      for (i in Array)
        objDict.Add(i, Array[i]);
      return objDict.Items();
    }
    
    function CreateVarArray()
    {
      var jsArray = [];
      jsArray[0] = "value 1";
      jsArray[1] = "value 2";
    
      varArray = ConvertArray(jsArray);
    }

     

     

  • easy-soft's avatar
    easy-soft
    Occasional Contributor

    Okay, I can answer my own questions. Actually the solution is given in the documentation for CreateVariantArray: 

     

     

    function ConvertArray(Array)
    {
      // Uses the Dictionary object to convert an array
      var objDict = getActiveXObject("Scripting.Dictionary");
      objDict.RemoveAll();
      for (i in Array)
        objDict.Add(i, Array[i]);
      return objDict.Items();
    }
    
    function CreateVarArray()
    {
      var jsArray = [];
      jsArray[0] = "value 1";
      jsArray[1] = "value 2";
    
      varArray = ConvertArray(jsArray);
    }

     

     

    • hkim5's avatar
      hkim5
      Staff

      you can even create dictionaries if you need!