Node: import object array from another js file?

前端 未结 5 1036
伪装坚强ぢ
伪装坚强ぢ 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 15:00

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

提交回复
热议问题