arraysdelphimultidimensional-arraytms-web-core

How does the TJSArray work in TMS WEB Core using only Delphi code?


I have the following code that creates a TJSArray with two location (latitude and longitude) coordinates in it:

var
  jsArray: TJSArray;
begin
  jsArray := TJSArray.New;
  asm
    jsArray = [
      [-29.1252991, 26.1627625],
      [-29.1228004, 26.1614185]
    ];
  end;
end;

The above code works perfectly fine, but I'm declaring the TJSArray in Delphi and then putting the data into it using JavaScript. I would prefer to use only Delphi, but I don't know how the TJSArray works in Delphi.

I'm able to push a single item into it with jsArray.push(), but I don't think that's what I need as I need to push multiple items into it.

How can I turn the above code into only Delphi code?


Solution

  • As far as I can see, the way the TJSArray works is only through the push() procedure and it has to push some kind of TJSValue into it.

    So to do the above code in Delphi, you can do the following:

    var
      jsArray: TJSArray;
      Coordinates: Array[0..1] of Double;
    begin
      jsArray := TJSArray.New;
    
      Coordinates[0] := -29.1252991;
      Coordinates[1] := 6.1627625;
      jsArray.push(JS.toArray(Coordinates));
    
      Coordinates[0] := -29.1228004;
      Coordinates[1] := 26.1614185;
      jsArray.push(JS.toArray(Coordinates));
    end;
    

    But at this point, the code is so much longer and uglier, but in order to push a second array into the TJSArray, you will have to create a full second array and use the JS.toArray method to turn it into the appropriate type for the push() procedure.

    This sucks, but I haven't found a better way yet.