I'm building a Node.js addon using Node-API.
Basically, my algorithm takes a js array as the input and then process it inside the addon and return it.
To do any logic for the array I need to loop through it. But I don't know-how. Because I didn't find any array iteration related documents in their documentation or in examples.
So I think this is more about doing it in C
. I have added what I tried on the below code, but not working and I have commented out it in the below code.
I also tried to find anything useful inside nodejs source code and node-addon-api but since my knowledge of this is limited, I didn't.
Please give me guidance on how to loop and access each object inside that array.
napi_value avm_simplify(napi_env env, napi_callback_info info) {
napi_status status;
napi_value value;
size_t argc = 1;
napi_value args[1];
status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
STATUS_CHECK
if (argc < 1) {
napi_throw_type_error(env, NULL, WRONG_ARGUMENTS_ERROR);
return NULL;
}
bool is_array = 0;
status = napi_is_array(env, args[0], &is_array);
STATUS_CHECK
if (!is_array) {
napi_throw_type_error(env, NULL, "Argument type should be an array");
return NULL;
}
// args[0] equals to something like
// [1, 2, 3, 4, 5]
// which is the user input (js array)
napi_value array = args[0];
/*
// This is what I wan't to do.
int length = sizeof(array) / sizeof(array[0]);
for (size_t i = 0; i < length; i++) {
napi_value object = array[i];
// do some logic with object
}
*/
return value;
}
I know this should be much easier using C++
, but my module mostly refers to C
libraries in future.
Thanks in advance.