【问题标题】:JS find a dynamic number of averages based on value in adjacent columnJS根据相邻列中的值查找动态平均值
【发布时间】:2021-09-09 19:41:40
【问题描述】:

编码新手并尝试解决此问题。我有一个带有这些列标题的 HTML 数据表:

大陆、国家、城市、平均气温

表格数据很长,有很多国家——每个国家有多个条目(即不同的城市)。 我想计算每个国家/地区的平均温度——即将具有相同国家/地区的所有条目的平均温度列相加,然后除以该条目数。

我的目标是最终得到两个数组,它们可以插入到 X 轴为“Country”和 Y 轴为“Avg Temp”的图表中。

我相信以下代码适用于其中一些,但我必须为每个国家(有数百个国家)复制并粘贴此代码。

//read the data from the HTML table
   var data = document.getElementById("myTable");

//find array values that match a country and push to new array, then convert array to integer
          var canada= [];
          var j = 0;
          for (j; j < data.rows.length; j++) {
             if ( data.rows[j].cells[1].innerHTML.indexOf("Canada") >= 0) 
                 canada.push(data.rows[j].cells[3].innerHTML);
           {

//sum the array for a country, find average, and push to a new array to plug into a chart
            function getSum(total, num) {
               return total + num;
               }
            myChartData.push(canada.reduce(getSum,0)/canada.length);

我想知道是否可以从 Country 列创建一个包含所有唯一国家/地区值的数组,然后以某种方式为该数组中的每个值执行上述代码。有什么想法吗?或者更好的方法来做到这一点?

【问题讨论】:

  • 你知道列表中的所有国家吗?然后,您可以创建一个国家列表并遍历每个值。然后你可以得到每个的平均值。
  • 请回复,已经超过45分钟了

标签: javascript arrays dynamic average


【解决方案1】:

好吧,我在这里找到了两种解决方案,一种是简单的嵌套循环,另一种是更动态的方法。假设你的代码的其余部分工作它应该是这样的。

  1. 嵌套循环。创建所有可能国家的列表,然后您可以为各个国家运行该功能。 O(n^2)

    //create a list of countries 
    const countries = ["Canada", "India", "Austrailia"]; 
    
    for (const element of countries){
     console.log(element);
    
     var temperatures = [] 
     for (j = 0 ; j < data.rows.length; j++){
         if (data.rows[j].cells[1].innerHTML.indexOf(element) >= 0) 
             temperatures.push(data.rows[j].cells[3].innerHTML);
     }    
     //assuming this was an array defined before. you can plot countries to myChartData
     myChartData.push(temperatures.reduce(getSum,0)/temperatures.length); 
    }
    
  2. 动态使用地图。 (O)(n) + 一点点

     //list of countries to simulate data (country, temp)
     const data = [["Canada",12], ["India",10], ["Austrailia",20],
                 ["Canada",0],["Canada",18],["India",10]]; 
    
     let countryTemp = new Map(); 
     for (const element of data){
     //element[1] is data.rows[j].cells[3].innerHTML) or temp
     //element[0] is data.rows[j].cells[1].innerHTML) or country
         var temp = []; 
         //if exists collect list 
         if(countryTemp.has(element[0])) 
             temp = countryTemp.get(element[0])
         //append to list of tmepratures 
         temp.push(element[1]);       
         countryTemp.set(element[0],temp); 
     }
    
    
      //u can plot country list to avg temp list. 
     var countryList = []; 
     var avgTemperaturelist =[]; 
     for(const val of countryTemp){
          countryList.push(val[0]); 
          avgTemperaturelist.push(val[1].reduce(getSum,0)/val[1].length); 
     }
    

提示:你可以通过在地图中使用 (sum and len(aunto-increment) 而不是我刚刚使用你的数组逻辑的列表来加快速度

【讨论】:

  • 谢谢!抱歉耽搁了(没有意识到stackoverflow移动得这么快:D)。你的第一个例子对我有用。我仍在完善图表所需的输出,但我现在可以使用您的代码以数组形式获取每个国家/地区的平均温度。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-10
  • 2021-01-27
相关资源
最近更新 更多