首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Node-API迭代JavaScript数组

如何使用Node-API迭代JavaScript数组
EN

Stack Overflow用户
提问于 2022-01-22 06:14:32
回答 1查看 252关注 0票数 1

我正在使用Node.js构建一个节点-API插件。

基本上,我的算法以一个js数组作为输入,然后在插件中处理它并返回它。

要对数组执行任何逻辑,我需要遍历它。但我不知道-怎么做。因为我没有在它们的文档或示例中找到任何与数组迭代相关的文档。

因此,我认为这更多的是在C中进行。我在下面的代码中添加了我尝试过的内容,但没有工作,我在下面的代码中对它进行了注释。

我还试图在nodejs源代码节点-addon-api中找到有用的东西,但由于我对此的了解有限,所以我没有。

请指导我如何循环和访问数组中的每个对象。

代码语言:javascript
复制
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;
}

我知道使用C++应该容易得多,但是我的模块在将来主要引用C库。

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-22 07:41:17

我发现https://nodejs.org/api/n-api.html是个不错的读物

下面是我将如何迭代(添加状态检查)

代码语言:javascript
复制
uint32_t length;
status = napi_get_array_length(env, array, &length);
for (uint32_t i = 0; i < length; ++i) {
  napi_value ret;
  napi_get_element(env, array, i, &ret);

  int32_t myint;
  napi_get_value_int32(env, ret, &myint);

下面的示例中,输入数组的每个元素都在适当的位置上重复2次

代码语言:javascript
复制
// hello.cc using Node-API
#include <node_api.h>
#include <assert.h>
namespace demo {

napi_value Method(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);
  assert(status == napi_ok);

  if (argc < 1) {
    napi_throw_type_error(env, NULL, "Wrong number of arguments");
    return NULL;
  }

  bool is_array = 0;
  status = napi_is_array(env, args[0], &is_array);
  assert(status == napi_ok);

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

  if (!array) {
    std::cout<<"array is null"<<std::endl;
    return NULL;
  }


  uint32_t length;
  status = napi_get_array_length(env, array, &length);

  for (uint32_t i = 0; i < length; ++i) {
    napi_value ret;
    napi_get_element(env, array, i, &ret);

    int32_t myint;
    napi_get_value_int32(env, ret, &myint);


    napi_value result;
    napi_create_int32(env, myint * 2, &result);
    napi_set_element(env, array, i, result);
  }

  return array;
}

// copy pasted
napi_value init(napi_env env, napi_value exports) {
  napi_status status;
  napi_value fn;

  status = napi_create_function(env, nullptr, 0, Method, nullptr, &fn);
  if (status != napi_ok) return nullptr;

  status = napi_set_named_property(env, exports, "hello", fn);
  if (status != napi_ok) return nullptr;
  return exports;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, init)

}  // namespace demo
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70810446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档