node.js 节点:从另一个 js 文件导入对象数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23897429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:24:14  来源:igfitidea点击:

Node: import object array from another js file?

node.jsexpress

提问by vantage5353

In a file called data.js, I have a big object array:

在名为 data.js 的文件中,我有一个大对象数组:

var arr = [ {prop1: value, prop2: value},...]

I'd like to use this array into my Node.js app, but code like

我想将此数组用于我的 Node.js 应用程序,但代码如下

require('./data.js')

doesn't help. I know how to export functions, but I'm lost when it comes to "importing" arrays. How do I add the data.js file to my app.js?

没有帮助。我知道如何导出函数,但是在“导入”数组时我迷路了。如何将 data.js 文件添加到我的 app.js 中?

回答by muffel

Local variables (var whatever) are not exported and local to the module. You can define your array on the exports object in order to allow to import it. You could create a .json file as well, if your array only contains simple objects.

局部变量(var 什么的)不会被导出并且是模块的局部变量。您可以在导出对象上定义数组以允许导入它。如果您的数组只包含简单对象,您也可以创建一个 .json 文件。

data.js:

数据.js:

module.exports = ['foo','bar',3];

import.js

导入.js

console.log(require('./data')); //output: [ 'foo', 'bar', 3 ]

[Edit]

[编辑]

If you require a module (for the first time), its code is executed and the exportsobject is returned and cached. For all further calls to require(), only the cached context is returned.

如果你需要一个模块(第一次),它的代码会被执行,exports对象会被返回和缓存。对于对 的所有进一步调用require(),仅返回缓存的上下文。

You can nevertheless modify objects from within a modules code. Consider this module:

尽管如此,您仍可以从模块代码中修改对象。考虑这个模块:

module.exports.arr = [];
module.exports.push2arr = function(val){module.exports.arr.push(val);};

and calling code:

和调用代码:

var mod = require("./mymodule");
mod.push2arr(2);
mod.push2arr(3);
console.log(mod.arr); //output: [2,3]

回答by Jonathan Lonowski

I know how to export functions, [...]

我知道如何导出函数,[...]

An array, or really any value, can be exported the same as functions, by modifying module.exportsor exports:

function通过修改module.exports或,可以像s一样导出数组或任何值exports

var arr = [ {prop1: value, prop2: value},...];

exports.arr = arr;
var arr = require('./data').arr;

回答by Harshit Anand

You can directly get JSON or JSON array from any file in NODEJS there is no need to export it from a JS script

您可以直接从 NODEJS 中的任何文件中获取 JSON 或 JSON 数组,无需从 JS 脚本中导出

    [ {
        prop1: value, 
        prop2: value
      },
      {
        prop1: val,
        prop2: val2
      },
      ...
   ]

Store it to a JSON file suppose test.json

将它存储到一个 JSON 文件假设test.json

Now you can export it as given below its very simple.

现在您可以将其导出如下,非常简单。

let data = require('./test.json');

回答by HynekS

You can simply wrap your array in a function.

您可以简单地将数组包装在一个函数中。

// myArray.js
export default function iDoReturnAnArray() {
  return ['foo', 'bar', 'baz'];
}

// main.js 
import iDoReturnAnArray from './path/to/myArray';

const unwrappedArray = iDoReturnAnArray();

console.log(unwrappedArray); // ['foo', 'bar', 'baz']

回答by Ashkon

We can use destructuring to solve your problem.

我们可以使用解构来解决您的问题。

In file1.js, we create an object array:

在 file1.js 中,我们创建了一个对象数组:

var arr = [{ prop1: "beep", prop2: "boop" }];

Then we do the following to export:

然后我们执行以下操作来导出:

module.exports = { arr };

Then in another file, file2.js, we require the array from file1

然后在另一个文件 file2.js 中,我们需要来自 file1 的数组

const f1 = require("./file1");

Log the result to test

记录结果以进行测试

console.log(f1.arr);