Sample code for 30+ languages & platforms
AutoIt

Call a JavaScript Function Returning an Array

See more JavaScript Examples

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

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

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

$oSbScript = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oSbScript.LoadFile("js_function_returning_array.js","utf-8")
If ($bSuccess = False) Then
    ConsoleWrite($oSbScript.LastErrorText & @CRLF)
    Exit
EndIf

$oJs = ObjCreate("Chilkat.Js")

$oResult = ObjCreate("Chilkat.JsonObject")
$oResult.EmitCompact = False

;  Call Eval to add the function to the context's global object
$bSuccess = $oJs.Eval($oSbScript,$oResult)
If ($bSuccess = False) Then
    ;  Examine the result for an exception.
    ConsoleWrite($oResult.Emit() & @CRLF)

    ;  Also examine the LastErrorText.
    ConsoleWrite($oJs.LastErrorText & @CRLF)
    Exit
EndIf

;  ------------------------------------------------------------------------------
;  Call each function

$oFuncCall = ObjCreate("Chilkat.JsonObject")

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

$oFuncCall.UpdateString("name","getDays")

$bSuccess = $oJs.CallFunction($oFuncCall,$oResult)
If ($bSuccess = False) Then
    ;  Examine the result for an exception.
    ConsoleWrite($oResult.Emit() & @CRLF)

    ;  Also examine the LastErrorText.
    ConsoleWrite($oJs.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite($oResult.Emit() & @CRLF)

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

;  Access each array value..
Local $iCount = $oResult.SizeOfArray("value")
Local $i = 0
While $i < $iCount
    $oResult.I = $i
    ConsoleWrite($oResult.StringOf("value[i]") & @CRLF)
    $i = $i + 1
Wend

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

$oFuncCall.Clear 
$oFuncCall.UpdateString("name","getRange")
$oFuncCall.UpdateInt("args[0]",14)
$oFuncCall.UpdateInt("args[1]",21)
$bSuccess = $oJs.CallFunction($oFuncCall,$oResult)
ConsoleWrite($oResult.Emit() & @CRLF)

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

$iCount = $oResult.SizeOfArray("value")
$i = 0
While $i < $iCount
    $oResult.I = $i
    ConsoleWrite($oResult.IntOf("value[i]") & @CRLF)
    $i = $i + 1
Wend

;  ------------------------------------------------------------------------------
;  Call the getEmployees() function

$oFuncCall.Clear 
$oFuncCall.UpdateString("name","getEmployees")
$bSuccess = $oJs.CallFunction($oFuncCall,$oResult)
ConsoleWrite($oResult.Emit() & @CRLF)

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

$iCount = $oResult.SizeOfArray("value")
$i = 0
While $i < $iCount
    $oResult.I = $i
    ConsoleWrite("name: " & $oResult.StringOf("value[i].name") & @CRLF)
    ConsoleWrite("role: " & $oResult.StringOf("value[i].role") & @CRLF)
    ConsoleWrite("id: " & $oResult.IntOf("value[i].id") & @CRLF)
    ConsoleWrite("" & @CRLF)
    $i = $i + 1
Wend
JavaScript

function getDays() {
    return ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
}

function getRange(start, end) {
    var arr = [];
    for (var i = start; i <= end; i++) {
        arr.push(i);
    }
    return arr;
}
// Usage: getRange(1, 5) -> [1, 2, 3, 4, 5]

function getEmployees() {
    return [
        { id: 101, name: "Alice", role: "Dev" },
        { id: 102, name: "Bob", role: "Manager" }
    ];
}