简体   繁体   English

如何将查询响应分配给 global.arrays,而不是 global.arrays 数组中的值?

[英]How to assign the query responses to the global.arrays, not the value inside the array of global.arrays?

QUESTION:问题:

I am trying to assign to global arrays the results of queries to my MongoDB database efficiently.我正在尝试有效地将查询结果分配给全局 arrays 到我的 MongoDB 数据库。 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.我基本上试图将对全局 arrays 的引用存储在一个数组中,以便我可以将 for 循环中的查询结果分配给所有这些引用。

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: 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

NB:注意:

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

    })

When you do this:当你这样做时:

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

you copy the value in global.array1 etc. into arrays .您将global.array1等中的值复制arrays中。 That value is called an object reference.该值称为 object 参考。 It tells the JavaScript engine where the array is, elsewhere in memory.它告诉 JavaScript 引擎阵列在哪里,在 memory 的其他地方。 There is no ongoing link between arrays[0] and global.array1 (for instance); arrays[0]global.array1之间没有持续的链接(例如); they both just happen to contain the same value.它们都恰好包含相同的值。

Your code replaces the values in arrays .您的代码替换arrays中的值。 This has no effect at all on global.array1 etc.这对global.array1等完全没有影响。

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:如果您确实有一个名为global的 object ,那么您可以使用括号表示法对其进行索引(请参阅此问题的答案)以实际更改global.array1的值,如下所示:

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:或者,如果名称不是这样的数字,那么您将使用colsArray来获取名称:

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.但我强烈建议您根本不要使用全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM