Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing an array in javascript file and using it in another file

I have researched this problem and have not found a simple solution, or it has not been broken down into something I can understand. This should be extremely simple but it is not.

I need to store data (array) in one file (file1)

const myArray= new Array['data0', 'data1', 'data2'];
module.exports = { myArray};

the inside of the second file I try to import it and call it like this (file2)

import myArray from './file1';
console.log myarray[0, 1, 2]
like image 326
Jason V Avatar asked Oct 28 '25 02:10

Jason V


1 Answers

I'll be honest with you, I can't think of a usecase in which you would need to export/import a simple string array. Exporting an object (containing an array, perhaps) has a bit more utility. But the following should work.

// file1.mjs
const myArray = ['data0', 'data1', 'data2'];
export default myArray;
// file2.mjs
import myArray from './file1.mjs';
console.log('myArray =>', myArray);
like image 137
astangelo Avatar answered Oct 30 '25 18:10

astangelo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!