Sample code for 30+ languages & platforms
Visual Basic 6.0

Call a JavaScript Function Returning an Array

See more JavaScript Examples

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

Chilkat Visual Basic 6.0 Downloads

Visual Basic 6.0
Dim success As Long
success = 0

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

Dim sbScript As New ChilkatStringBuilder
success = sbScript.LoadFile("js_function_returning_array.js","utf-8")
If (success = 0) Then
    Debug.Print sbScript.LastErrorText
    Exit Sub
End If

Dim js As New ChilkatJs

Dim result As New ChilkatJsonObject
result.EmitCompact = 0

'  Call Eval to add the function to the context's global object
success = js.Eval(sbScript,result)
If (success = 0) Then
    '  Examine the result for an exception.
    Debug.Print result.Emit()

    '  Also examine the LastErrorText.
    Debug.Print js.LastErrorText
    Exit Sub
End If

'  ------------------------------------------------------------------------------
'  Call each function

Dim funcCall As New ChilkatJsonObject

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

success = funcCall.UpdateString("name","getDays")

success = js.CallFunction(funcCall,result)
If (success = 0) Then
    '  Examine the result for an exception.
    Debug.Print result.Emit()

    '  Also examine the LastErrorText.
    Debug.Print js.LastErrorText
    Exit Sub
End If

Debug.Print result.Emit()

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

'  Access each array value..
Dim count As Long
count = result.SizeOfArray("value")
Dim i As Long
i = 0
Do While i < count
    result.I = i
    Debug.Print result.StringOf("value[i]")
    i = i + 1
Loop

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

funcCall.Clear 
success = funcCall.UpdateString("name","getRange")
success = funcCall.UpdateInt("args[0]",14)
success = funcCall.UpdateInt("args[1]",21)
success = js.CallFunction(funcCall,result)
Debug.Print result.Emit()

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

count = result.SizeOfArray("value")
i = 0
Do While i < count
    result.I = i
    Debug.Print result.IntOf("value[i]")
    i = i + 1
Loop

'  ------------------------------------------------------------------------------
'  Call the getEmployees() function

funcCall.Clear 
success = funcCall.UpdateString("name","getEmployees")
success = js.CallFunction(funcCall,result)
Debug.Print result.Emit()

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

count = result.SizeOfArray("value")
i = 0
Do While i < count
    result.I = i
    Debug.Print "name: " & result.StringOf("value[i].name")
    Debug.Print "role: " & result.StringOf("value[i].role")
    Debug.Print "id: " & result.IntOf("value[i].id")
    Debug.Print ""
    i = i + 1
Loop
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" }
    ];
}