【问题标题】:Variable assigned to array not updating when array does (JS)分配给数组的变量在数组更新时不更新(JS)
【发布时间】:2018-03-06 19:08:20
【问题描述】:

我正在分配一个变量,代理,等于一个数组。我正在尝试稍后将项目添加到该数组中,然后在其他文件中访问该数组。

问题是,proxyHelper.proxies 的值没有更新以反映代理变量的值。

修改数组后,console.log(proxies)返回修改后的数组,但是console.log(proxyHelper.proxies)返回空白。我需要访问其他文件中的 proxyHelper.proxies 值,这样您就可以看到为什么会出现问题。

我在其他地方使用了类似的代码,它工作正常 - 我没有看到什么?

var proxies = [];

proxyHelper.proxies = proxies;

proxyHelper.tester = function(win) {

    electron.ipcMain.on('saveProxies', function(event, data) {
        // Clear the previous proxies from list
        proxies = [];
        // Split proxies based on line breaks
        if (data != '') {
            let proxiesList = data.split('\n');
            // i<= because we want to run a check to see if all proxies are added
            for (let i = 0; i <= proxiesList.length; i++) {
                // if the for loop hasn't ran through all proxies
                if (i + 1 <= proxiesList.length) {
                    proxies.push(proxiesList[i]);
                }
                // Once it's loop through all proxies
                else {
                    //Returns nothing
                    console.log(proxyHelper.proxies);
                    //Returns array with added items
                    console.log(proxies);
                    win.webContents.send('goodProxies', 'Saved!');
                }
            }
        } else {
            win.webContents.send('emptyProxies', 'Empty.');
        }
    })
}

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:
    proxies = [];
    

    您刚刚为该变量分配了一个新数组。

    proxyHelper.proxies 仍然指向之前的值,不受影响。

    您应该始终使用单个变量,或者改变它而不是重新分配它。

    【讨论】:

      【解决方案2】:

      这是您的代码中发生的事情:

      var proxies = []; // This new variable ("proxies") contains a reference to an empty array.
      
      proxyHelper.proxies = proxies; // You assign the same reference to a property 
                                     // (named "proxies") of the "proxyHelper"
                                     // object. The "proxies" property points
                                     // to the same empty array as the variable above.
      
      proxyHelper.tester = function(win) {
      
          electron.ipcMain.on('saveProxies', function(event, data) {
      
              proxies = []; // You assign a new reference to the global variable
                            // "proxies", which points to a new empty array.
                            // At this point the reference assigned to
                            // "proxyHelper.proxies" is still the original
                            // empty array from line 1
              // ...
          }
      
          // ...
      }
      

      因此,当您访问“proxyHelper.proxies”时,您始终访问的是从未修改过的原始空数组...

      你应该做的是:

      proxyHelper.proxies = []; // resets the object's property to a new empty array
      

      【讨论】:

        【解决方案3】:

        不需要proxy 变量,因为它的更改不会反映到proxyHelper.proxies
        只需使用 proxyHelper.proxies 本身
        我还稍微清理了你的代码
        您可以使用很多 Array 方法,而不是 for 循环

        proxyHelper.proxies = []
        
        proxyHelper.tester = function(win) {
        
            electron.ipcMain.on('saveProxies', function(event, data) {
                //Split proxies based on line breaks
                if (data != '') {
                    let proxiesList = data.split('\n');
                    proxiesList.forEach(function(proxy) {
                        proxyHelper.proxies.push(proxy)
                    })
                    win.webContents.send('goodProxies', 'Saved!');
                } else {
                    win.webContents.send('emptyProxies', 'Empty.');
                }
            })
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-11-27
          • 2021-08-30
          • 1970-01-01
          • 1970-01-01
          • 2020-11-19
          • 1970-01-01
          • 2018-07-30
          相关资源
          最近更新 更多