Sample code for 30+ languages & platforms
Delphi DLL

Call a JavaScript Function Returning an Array

See more JavaScript Examples

Demonstrates how to call a JavaScript function that returns an array.

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Js, StringBuilder, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
sbScript: HCkStringBuilder;
js: HCkJs;
result: HCkJsonObject;
funcCall: HCkJsonObject;
count: Integer;
i: Integer;

begin
success := False;

// ----------------------------------------------------------------------------------
// The Javascript functions called in this example are shown at the bottom of this page.
// -----------------------------------------------------------------------------------

sbScript := CkStringBuilder_Create();
success := CkStringBuilder_LoadFile(sbScript,'js_function_returning_array.js','utf-8');
if (success = False) then
  begin
    Memo1.Lines.Add(CkStringBuilder__lastErrorText(sbScript));
    Exit;
  end;

js := CkJs_Create();

result := CkJsonObject_Create();
CkJsonObject_putEmitCompact(result,False);

// Call Eval to add the function to the context's global object
success := CkJs_Eval(js,sbScript,result);
if (success = False) then
  begin
    // Examine the result for an exception.
    Memo1.Lines.Add(CkJsonObject__emit(result));

    // Also examine the LastErrorText.
    Memo1.Lines.Add(CkJs__lastErrorText(js));
    Exit;
  end;

// ------------------------------------------------------------------------------
// Call each function

funcCall := CkJsonObject_Create();

// Create JSON specifying the function name and arguments
// The function has no arguments, so we only specify the name.

CkJsonObject_UpdateString(funcCall,'name','getDays');

success := CkJs_CallFunction(js,funcCall,result);
if (success = False) then
  begin
    // Examine the result for an exception.
    Memo1.Lines.Add(CkJsonObject__emit(result));

    // Also examine the LastErrorText.
    Memo1.Lines.Add(CkJs__lastErrorText(js));
    Exit;
  end;

Memo1.Lines.Add(CkJsonObject__emit(result));

// Output:
// {
//   "type": "array",
//   "value": [
//     "Monday",
//     "Tuesday",
//     "Wednesday",
//     "Thursday",
//     "Friday"
//   ]
// }

// Access each array value..
count := CkJsonObject_SizeOfArray(result,'value');
i := 0;
while i < count do
  begin
    CkJsonObject_putI(result,i);
    Memo1.Lines.Add(CkJsonObject__stringOf(result,'value[i]'));
    i := i + 1;
  end;

// ------------------------------------------------------------------------------
// Call the getRange(start,end) function

CkJsonObject_Clear(funcCall);
CkJsonObject_UpdateString(funcCall,'name','getRange');
CkJsonObject_UpdateInt(funcCall,'args[0]',14);
CkJsonObject_UpdateInt(funcCall,'args[1]',21);
success := CkJs_CallFunction(js,funcCall,result);
Memo1.Lines.Add(CkJsonObject__emit(result));

// Output:
// {
//   "type": "array",
//   "value": [
//     14,
//     15,
//     16,
//     17,
//     18,
//     19,
//     20,
//     21
//   ]
// }

count := CkJsonObject_SizeOfArray(result,'value');
i := 0;
while i < count do
  begin
    CkJsonObject_putI(result,i);
    Memo1.Lines.Add(IntToStr(CkJsonObject_IntOf(result,'value[i]')));
    i := i + 1;
  end;

// ------------------------------------------------------------------------------
// Call the getEmployees() function

CkJsonObject_Clear(funcCall);
CkJsonObject_UpdateString(funcCall,'name','getEmployees');
success := CkJs_CallFunction(js,funcCall,result);
Memo1.Lines.Add(CkJsonObject__emit(result));

// Output:
// {
//   "type": "array",
//   "value": [
//     {
//       "id": 101,
//       "name": "Alice",
//       "role": "Dev"
//     },
//     {
//       "id": 102,
//       "name": "Bob",
//       "role": "Manager"
//     }
//   ]
// }

count := CkJsonObject_SizeOfArray(result,'value');
i := 0;
while i < count do
  begin
    CkJsonObject_putI(result,i);
    Memo1.Lines.Add('name: ' + CkJsonObject__stringOf(result,'value[i].name'));
    Memo1.Lines.Add('role: ' + CkJsonObject__stringOf(result,'value[i].role'));
    Memo1.Lines.Add('id: ' + IntToStr(CkJsonObject_IntOf(result,'value[i].id')));
    Memo1.Lines.Add('');
    i := i + 1;
  end;

CkStringBuilder_Dispose(sbScript);
CkJs_Dispose(js);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(funcCall);

end;