React Native Requires Chilkat v11.4.0+
React Native
Call a JavaScript Function Returning an Array
See more JavaScript Examples
Demonstrates how to call a JavaScript function that returns an array.Chilkat React Native Downloads
import { Js, JsonObject, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
// ----------------------------------------------------------------------------------
// The Javascript functions called in this example are shown at the bottom of this page.
// -----------------------------------------------------------------------------------
const sbScript = new StringBuilder();
try {
sbScript.loadFile('js_function_returning_array.js', 'utf-8');
} catch {
console.log(sbScript.lastErrorText);
return;
}
const js = new Js();
const result = new JsonObject();
result.emitCompact = false;
// Call Eval to add the function to the context's global object
try {
await js.evalAsync(sbScript, result);
} catch {
// Examine the result for an exception.
console.log(result.emit());
// Also examine the LastErrorText.
console.log(js.lastErrorText);
return;
}
// ------------------------------------------------------------------------------
// Call each function
const funcCall = new JsonObject();
// Create JSON specifying the function name and arguments
// The function has no arguments, so we only specify the name.
funcCall.updateString('name', 'getDays');
try {
await js.callFunctionAsync(funcCall, result);
} catch {
// Examine the result for an exception.
console.log(result.emit());
// Also examine the LastErrorText.
console.log(js.lastErrorText);
return;
}
console.log(result.emit());
// Output:
// {
// "type": "array",
// "value": [
// "Monday",
// "Tuesday",
// "Wednesday",
// "Thursday",
// "Friday"
// ]
// }
// Access each array value..
let count = result.sizeOfArray('value');
let i = 0;
while (i < count) {
result.i = i;
console.log(result.stringOf('value[i]'));
i++;
}
// ------------------------------------------------------------------------------
// Call the getRange(start,end) function
funcCall.clear();
funcCall.updateString('name', 'getRange');
funcCall.updateInt('args[0]', 14);
funcCall.updateInt('args[1]', 21);
await js.callFunctionAsync(funcCall, result);
console.log(result.emit());
// Output:
// {
// "type": "array",
// "value": [
// 14,
// 15,
// 16,
// 17,
// 18,
// 19,
// 20,
// 21
// ]
// }
count = result.sizeOfArray('value');
i = 0;
while (i < count) {
result.i = i;
console.log(result.intOf('value[i]'));
i++;
}
// ------------------------------------------------------------------------------
// Call the getEmployees() function
funcCall.clear();
funcCall.updateString('name', 'getEmployees');
await js.callFunctionAsync(funcCall, result);
console.log(result.emit());
// Output:
// {
// "type": "array",
// "value": [
// {
// "id": 101,
// "name": "Alice",
// "role": "Dev"
// },
// {
// "id": 102,
// "name": "Bob",
// "role": "Manager"
// }
// ]
// }
count = result.sizeOfArray('value');
i = 0;
while (i < count) {
result.i = i;
console.log(`name: ${result.stringOf('value[i].name')}`);
console.log(`role: ${result.stringOf('value[i].role')}`);
console.log(`id: ${result.intOf('value[i].id')}`);
console.log('');
i++;
}
}
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" }
];
}