1

QUESTION:

I am trying to assign to global arrays the results of queries to my MongoDB database efficiently. I essentially tried to store the references to the global arrays inside an array so that I could assign to all of them the results of the queries inside a for loop.

This does not seem to be possible. What would you suggest ?


CODE:

    var arrays = [global.array1, global.array2, global.array3];

    var colsArray = ["array1","array2","array3"];

    var promises = colsArray.map(col => global.fetchCollection(col));

    Promise.all(promises).then(responses => {

        for (var d = 0; d < responses.length; d++) {
            arrays[d] = responses[d];
        } 

        console.log("VALUE INSIDE ARRAY of global.arrays: "+arrays[0]);
        console.log("VALUE OF global.array is still : "+global.array1);

    })

OUTPUT:

VALUE INSIDE ARRAY of global.arrays:: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
VALUE OF global.array is still : undefined

N.B.:

This would work of course. But quite unsatisfactory of course:

        var arrays = [global.array1, global.array2, global.array3];

        var colsArray = ["array1","array2","array3"];

        var promises = colsArray.map(col => global.fetchCollection(col));

        Promise.all(promises).then(responses => {

                global.array1 = responses[0];
                global.array2 = responses[2];
                global.array3 = responses[3];

        })

EDIT:

This does not work:

    var arrays = [global.array1, global.array2, global.array3];

    var colsArray = ["array1","array2","array3"];

    var promises = colsArray.map(col => global.fetchCollection(col));

    Promise.all(promises).then(responses => {

        for (var d = 0; d < responses.length; d++) {
            arrays[colsArray[d]] = responses[d];
        } 

        console.log("VALUE INSIDE ARRAY of global.arrays: "+arrays[0]);
        console.log("VALUE OF global.array is still : "+global.array1);

    })
TheProgrammer
  • 1,409
  • 4
  • 24
  • 53

1 Answers1

1

When you do this:

var arrays = [global.array1, global.array2, global.array3];

you copy the value in global.array1 etc. into arrays. That value is called an object reference. It tells the JavaScript engine where the array is, elsewhere in memory. There is no ongoing link between arrays[0] and global.array1 (for instance); they both just happen to contain the same value.

Your code replaces the values in arrays. This has no effect at all on global.array1 etc.

If you really have an object called global, then you can index into it with brackets notation (see this question's answers) to actually change the value of global.array1 like this:

for (var d = 0; d < responses.length; d++) {
    global["array" + (d + 1)] = responses[d];
} 

Live Example:

var global = {
    fetchCollection(col) {
        return new Promise(resolve => {
            setTimeout(() => {
                resolve("Response for " + col);
            }, Math.floor(Math.random() * 800));
        });
    }
};

var colsArray = ["array1", "array2", "array3"];

var promises = colsArray.map(col => global.fetchCollection(col));

Promise.all(promises).then(responses => {
    for (var d = 0; d < responses.length; d++) {
        global["array" + (d + 1)] = responses[d];
    } 

    console.log("VALUE OF global.array1 is : " + global.array1);

});

Or if the names are not numeric like that, then you'd use your colsArray to get the name:

for (var d = 0; d < responses.length; d++) {
    global[colsArray[d]] = responses[d];
} 

Live Example:

var global = {
    fetchCollection(col) {
        return new Promise(resolve => {
            setTimeout(() => {
                resolve("Response for " + col);
            }, Math.floor(Math.random() * 800));
        });
    }
};

var colsArray = ["array1", "array2", "array3"];

var promises = colsArray.map(col => global.fetchCollection(col));

Promise.all(promises).then(responses => {
    for (var d = 0; d < responses.length; d++) {
        global[colsArray[d]] = responses[d];
    } 

    console.log("VALUE OF global.array1 is : " + global.array1);

});

But I'd strongly encourage you not to use globals at all.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875