如何在Node JS中使用module.exports导出数组? [英] How do I export an array with module.exports in node js?

查看:1069
本文介绍了如何在Node JS中使用module.exports导出数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用node.js的项目.这是我第一次使用nodejs,我想将数组导出到我的应用程序.这是一些代码:

I have a project using node.js. It's my first time using nodejs and I want to export an array to my app. Here is some code:

module.exports = { 
    var arrays = [];
    arrays[0] = 'array 0';
    arrays[1] = 'array 1';
    arrays[2] = 'array 2';
    arrays[3] = 'array 3';
    arrays[4] = 'array 4';
    var r_array = arrays[Math.floor(Math.random() * arrays.length)].toString();
}

最后,我想在我的app.js中使用var r_array,但我不知道如何使用.

At the end I want to use the var r_array in my app.js but I don't know how.

推荐答案

您想要定义一个函数,该函数返回数组的随机部分:

You'd want to define a function which returns the randomized portion of the array:

module.exports = {
  getRArray: function() {
    var arrays = [];
    arrays[0] = 'array 0';
    arrays[1] = 'array 1';
    arrays[2] = 'array 2';
    arrays[3] = 'array 3';
    arrays[4] = 'array 4';
    return arrays[Math.floor(Math.random()*arrays.length)];
  }
};

此外,您还应该将数组嵌入到函数中,以便它实际上返回某些内容.

Also you should embed the array into the function so it actually returns something.

这篇关于如何在Node JS中使用module.exports导出数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆