【问题标题】:Passing a var from a forEach to another function将 var 从 forEach 传递给另一个函数
【发布时间】:2020-06-16 03:10:32
【问题描述】:

我正在尝试将 varforEach 循环中获取到单独的函数。我的示例是从 google 图表函数中提取的,这可能是造成混淆的原因,但是当我尝试将 var 数组从我的 forEach 获取到图表的 arrayToDataTable 时,我收到 undefined 错误。这是我的代码:

function showInfo(data, tabletop) {
  //alert('Successfully processed!')
  console.log(data);
  var header = document.querySelector('.header');
  var bodytext = document.querySelector('.bodytext');
  let i = 1;
  data.forEach(function(data) {
    var array = data.Name
    console.log(array)
  })
};


google.charts.load('current', {
  'packages': ['corechart']
});
google.charts.setOnLoadCallback(drawChart);

function drawChart(nameArrays, array) {
  var daa = google.visualization.arrayToDataTable([
    ['Name', 'Percentage'],
    ["'" + array + "'", 1],
    ["'" + array + "'", 1]
  ]);

  var options = {
    title: 'People Percentage',
    curveType: 'function',
    legend: {
      position: 'bottom'
    }
  };

  var chart = new google.visualization.PieChart(document.getElementById('curve_chart'));

  chart.draw(daa, options);
}

【问题讨论】:

  • 您正在将array 登录到控制台,但实际上并未将数据推送到数组中
  • 我不明白为什么 forEach 需要另一个函数
  • showInfo,你从console.log(data)得到了什么,你对console.log(array)的预期结果是什么?

标签: javascript function foreach


【解决方案1】:

您应该将数据从循环中保存到循环之外的数组/变量中,并确保它与声明要在其中使用值的函数的范围相同。

这是应用于您的解决方案的修复(仅包括它的基本区域)

var data = [ // Array of records for data, this would be the initial object that you are looping through
    { Name: "Bruh1" },
    { Name: "Bruh2" },
    { Name: "Bruh3" },
];
var arr = []; // Declare the array to take the data outside the loop
data.forEach((data) => {
    arr.push(data.Name); // Push data.Name into an array outside the forEach loop
});

// Putting that data into your other function
console.log([
      ['Name', 'Percentage'],
      ["'" + arr[0] + "'", 1],
      ["'" + arr[1] + "'", 1],
      ["'" + arr[2] + "'", 1]
]);

【讨论】:

    【解决方案2】:

    我认为这就是你应该解决这个问题的方式。您应该在 for 循环中调用 drawChart。我猜google.visualization.arrayToDataTable 不是异步函数调用

    function showInfo(data, tabletop) {
      //alert('Successfully processed!')
      console.log(data);
      var header = document.querySelector('.header');
      var bodytext = document.querySelector('.bodytext');
      let i = 1;
      data.forEach(function(data) {
        drawChart(nameArray, data.Name)
      })
    };
    
    
    google.charts.load('current', {
      'packages': ['corechart']
    });
    google.charts.setOnLoadCallback(drawChart);
    
    let daa;
    function drawChart(nameArrays, name) {
      daa = google.visualization.arrayToDataTable([
        ['Name', 'Percentage']
      ].push(["'" + name + "'", 1]));
    
      var options = {
        title: 'People Percentage',
        curveType: 'function',
        legend: {
          position: 'bottom'
        }
      };
    
      var chart = new google.visualization.PieChart(document.getElementById('curve_chart'));
    
      chart.draw(daa, options);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      • 2019-07-25
      • 2012-09-25
      相关资源
      最近更新 更多