Sample code for 30+ languages & platforms
Tcl

Call a JavaScript Function Passing an Array Argument

See more JavaScript Examples

Demonstrates how to call a JavaScript function with an argument that is an array.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  This is the JavaScript function we'll call:

#  function calculateTotal(numbers) {
#    console.log(numbers);
#    let total = 0;
#    
#    // Loop through every number in the array
#    for (const num of numbers) {
#      console.log(num);
#      total += num;
#    }
#    
#    return total;
#  }

set sbScript [new_CkStringBuilder]

set success [CkStringBuilder_LoadFile $sbScript "js_function_array_arg.js" "utf-8"]
if {$success == 0} then {
    puts [CkStringBuilder_lastErrorText $sbScript]
    delete_CkStringBuilder $sbScript
    exit
}

set js [new_CkJs]

set result [new_CkJsonObject]

CkJsonObject_put_EmitCompact $result 0

#  Call Eval to add the function to the context's global object
set success [CkJs_Eval $js $sbScript $result]
if {$success == 0} then {
    #  Examine the result for an exception.
    puts [CkJsonObject_emit $result]

    #  Also examine the LastErrorText.
    puts [CkJs_lastErrorText $js]
    delete_CkStringBuilder $sbScript
    delete_CkJs $js
    delete_CkJsonObject $result
    exit
}

#  ------------------------------------------------------------------------------
#  Call the function calculateTotal(numbers)

set funcCall [new_CkJsonObject]

CkJsonObject_put_EmitCompact $funcCall 0

#  Create JSON specifying the function name and arguments
#  In this case, there is only 1 argument, and it is an array.

CkJsonObject_UpdateString $funcCall "name" "calculateTotal"

#  Create the arguments array.
set argsArray [new_CkJsonArray]

#  The 1st argument in the arguments array is itself an array.
#  Passing -1 indicates to append to the array.
set arg [new_CkJsonArray]

CkJsonArray_AddArrayAt2 $argsArray -1 $arg

#  Fill in the values for the 1st argument.
CkJsonArray_AddNumberAt $arg -1 "10.50"
CkJsonArray_AddNumberAt $arg -1 "20.00"
CkJsonArray_AddNumberAt $arg -1 "5.25"

#  Add the "args" array to the funcCall.
CkJsonObject_AppendArrayCopy $funcCall "args" $argsArray

puts [CkJsonObject_emit $funcCall]

#  The funcCall is as follows.  Notice that the 1st (and only) argument is an array.

#  {
#    "name": "calculateTotal",
#    "args": [
#      [
#        10.50,
#        20.00,
#        5.25
#      ]
#    ]
#  }

set success [CkJs_CallFunction $js $funcCall $result]
if {$success == 0} then {
    #  Examine the result for an exception.
    puts [CkJsonObject_emit $result]

    #  Also examine the LastErrorText.
    puts [CkJs_lastErrorText $js]
    delete_CkStringBuilder $sbScript
    delete_CkJs $js
    delete_CkJsonObject $result
    delete_CkJsonObject $funcCall
    delete_CkJsonArray $argsArray
    delete_CkJsonArray $arg
    exit
}

puts [CkJsonObject_emit $result]

#  Result:
#  {
#    "type": "double",
#    "value": 35.75
#  }

#  The function also emitted text to the console.

set sbOut [new_CkStringBuilder]

CkJs_ConsoleOutputSb $js $sbOut
puts [CkStringBuilder_getAsString $sbOut]

#  Output:
#  10.5,20,5.25
#  10.5
#  20
#  5.25

#  -----------------------------------------------------------
#  Note: If the array argument is simple, this is an alternative
#  and simpler way of creating the funcCall:

CkJsonObject_Clear $funcCall
CkJsonObject_UpdateString $funcCall "name" "calculateTotal"
CkJsonObject_UpdateNumber $funcCall "args[0][0]" "10.50"
CkJsonObject_UpdateNumber $funcCall "args[0][1]" "20.00"
CkJsonObject_UpdateNumber $funcCall "args[0][2]" "5.25"
puts [CkJsonObject_emit $funcCall]

delete_CkStringBuilder $sbScript
delete_CkJs $js
delete_CkJsonObject $result
delete_CkJsonObject $funcCall
delete_CkJsonArray $argsArray
delete_CkJsonArray $arg
delete_CkStringBuilder $sbOut
JavaScript
function calculateTotal(numbers) {
  console.log(numbers);
  let total = 0;
  
  // Loop through every number in the array
  for (const num of numbers) {
    console.log(num);
    total += num;
  }
  
  return total;
}