Node: import object array from another js file?

前端 未结 5 1086
伪装坚强ぢ
伪装坚强ぢ 2021-02-02 14:13

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

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

I\'d like to use this array into my Node.js

5条回答
  •  耶瑟儿~
    2021-02-02 14:54

    We can use destructuring to solve your problem.

    In file1.js, we create an object array:

    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

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

    Log the result to test

    console.log(f1.arr);
    

提交回复
热议问题