Sample code for 30+ languages & platforms
PowerBuilder

Call a JavaScript Function Returning an Array

See more JavaScript Examples

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

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_SbScript
oleobject loo_Js
oleobject loo_Result
oleobject loo_FuncCall
integer li_Count
integer i

li_Success = 0

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

loo_SbScript = create oleobject
li_rc = loo_SbScript.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_SbScript
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_SbScript.LoadFile("js_function_returning_array.js","utf-8")
if li_Success = 0 then
    Write-Debug loo_SbScript.LastErrorText
    destroy loo_SbScript
    return
end if

loo_Js = create oleobject
li_rc = loo_Js.ConnectToNewObject("Chilkat.Js")

loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")

loo_Result.EmitCompact = 0

//  Call Eval to add the function to the context's global object
li_Success = loo_Js.Eval(loo_SbScript,loo_Result)
if li_Success = 0 then
    //  Examine the result for an exception.
    Write-Debug loo_Result.Emit()

    //  Also examine the LastErrorText.
    Write-Debug loo_Js.LastErrorText
    destroy loo_SbScript
    destroy loo_Js
    destroy loo_Result
    return
end if

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

loo_FuncCall = create oleobject
li_rc = loo_FuncCall.ConnectToNewObject("Chilkat.JsonObject")

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

loo_FuncCall.UpdateString("name","getDays")

li_Success = loo_Js.CallFunction(loo_FuncCall,loo_Result)
if li_Success = 0 then
    //  Examine the result for an exception.
    Write-Debug loo_Result.Emit()

    //  Also examine the LastErrorText.
    Write-Debug loo_Js.LastErrorText
    destroy loo_SbScript
    destroy loo_Js
    destroy loo_Result
    destroy loo_FuncCall
    return
end if

Write-Debug loo_Result.Emit()

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

//  Access each array value..
li_Count = loo_Result.SizeOfArray("value")
i = 0
do while i < li_Count
    loo_Result.I = i
    Write-Debug loo_Result.StringOf("value[i]")
    i = i + 1
loop

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

loo_FuncCall.Clear()
loo_FuncCall.UpdateString("name","getRange")
loo_FuncCall.UpdateInt("args[0]",14)
loo_FuncCall.UpdateInt("args[1]",21)
li_Success = loo_Js.CallFunction(loo_FuncCall,loo_Result)
Write-Debug loo_Result.Emit()

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

li_Count = loo_Result.SizeOfArray("value")
i = 0
do while i < li_Count
    loo_Result.I = i
    Write-Debug string(loo_Result.IntOf("value[i]"))
    i = i + 1
loop

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

loo_FuncCall.Clear()
loo_FuncCall.UpdateString("name","getEmployees")
li_Success = loo_Js.CallFunction(loo_FuncCall,loo_Result)
Write-Debug loo_Result.Emit()

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

li_Count = loo_Result.SizeOfArray("value")
i = 0
do while i < li_Count
    loo_Result.I = i
    Write-Debug "name: " + loo_Result.StringOf("value[i].name")
    Write-Debug "role: " + loo_Result.StringOf("value[i].role")
    Write-Debug "id: " + string(loo_Result.IntOf("value[i].id"))
    Write-Debug ""
    i = i + 1
loop


destroy loo_SbScript
destroy loo_Js
destroy loo_Result
destroy loo_FuncCall
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" }
    ];
}