Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.4.0+

Call a JavaScript Function Returning an Array

See more JavaScript Examples

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

Chilkat Rust Downloads

Rust

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

let sb_script = chilkat::StringBuilder::new();
if sb_script.load_file("js_function_returning_array.js", "utf-8").is_err() {
    println!("{}", sb_script.last_error_text());
    return;
}

let js = chilkat::Js::new();

let result = chilkat::JsonObject::new();
result.set_emit_compact(false);

// Call Eval to add the function to the context's global object
if js.eval(&sb_script, &result).is_err() {
    // Examine the result for an exception.
    println!("{}", result.emit().unwrap_or_default());

    // Also examine the LastErrorText.
    println!("{}", js.last_error_text());
    return;
}

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

let func_call = chilkat::JsonObject::new();

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

let _ = func_call.update_string("name", "getDays");

if js.call_function(&func_call, &result).is_err() {
    // Examine the result for an exception.
    println!("{}", result.emit().unwrap_or_default());

    // Also examine the LastErrorText.
    println!("{}", js.last_error_text());
    return;
}

println!("{}", result.emit().unwrap_or_default());

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

// Access each array value..
let mut count = result.size_of_array("value");
let mut i = 0;
while i < count {
    result.set_i(i);
    println!("{}", result.string_of("value[i]").unwrap_or_default());
    i = i + 1;
}

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

func_call.clear();
let _ = func_call.update_string("name", "getRange");
let _ = func_call.update_int("args[0]", 14);
let _ = func_call.update_int("args[1]", 21);
let _ = js.call_function(&func_call, &result).is_ok();
println!("{}", result.emit().unwrap_or_default());

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

count = result.size_of_array("value");
i = 0;
while i < count {
    result.set_i(i);
    println!("{}", result.int_of("value[i]"));
    i = i + 1;
}

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

func_call.clear();
let _ = func_call.update_string("name", "getEmployees");
let _ = js.call_function(&func_call, &result).is_ok();
println!("{}", result.emit().unwrap_or_default());

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

count = result.size_of_array("value");
i = 0;
while i < count {
    result.set_i(i);
    println!("name: {}", result.string_of("value[i].name").unwrap_or_default());
    println!("role: {}", result.string_of("value[i].role").unwrap_or_default());
    println!("id: {}", result.int_of("value[i].id"));
    println!("");
    i = i + 1;
}

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" }
    ];
}