13

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();
}

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

1
  • 1
    Please, use Arrays the proper / simpler way: const arrays = ["a", "b", /*etc*/ ]; Commented Apr 2, 2022 at 19:22

6 Answers 6

13

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.

Sign up to request clarification or add additional context in comments.

Comments

5

module.exports needs to be an object.

Perhaps you're looking for something more like:

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();

module.exports = r_array;

Please note that this code will only be run once, and that if you're hoping to get a different random value by executing the code multiple times, that you may want to set it up more like:

module.exports = function() {
  return arrays[Math.floor(Math.random()*arrays.length)];
}

so that the Math.random() operation happens over and over.

1 Comment

module.exports can export an array directly. It does not need to be an object. (Though, it is true that an array is a type of an object)
1

You have two files, one is array.js and the other app.js. Consider doing this in array.js file:

const Array= [
    'array 0',
    'array 1',
    'array 2',
    'array 3',
    'array 4']
   
exports.Array = Array 

Now in your app.js do the following:

const { Array } = require("./array")

const randomArr = Array[Math.floor(Math.random()*Array.length)]
console.log(randomArr)
 // I used randomArr  instead of r_array because of JS conventions.

Comments

0
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();
module.exports = r_array;

2 Comments

okay thank you! but i have an additional question. then there is only one random array. how do i have to do it if i want every time another array? :)
You should check @therobinkim answer. But note that you are not going to get a random array every time if you are applying toString to the result. This means the value will be translated into a string, so you'll get a random string every time.
0

Try it like this , if you are using type : "module" in package.json

const myArray = [
       {
            name: 'Cake',
            value: 'cake'
        },
        {
            name: 'Veg thali',
            value: 'vegthali'
        }
]

export default myArray

import myArray from "./arrayfile.js"

Comments

0

You can actually export an array directly, as array is also an object in javascript. Inside file that has the data (for ex, arrayData.js), you would have:

module.exports = [
  {key1: 'abc', key2: 123},
  {key1: 'def', key2: 456}  
];

Then in the code that wants to import the array, you can do:

// import array data
const importedArrayData = require('arrayData.js');
// access array data, for ex, log 2nd element key2
console.log(importedArrayData[1].key2); // output: 456

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.