我有一个百年的每个月的温度数据。如何计算每年的平均温度?

时间:2016-08-25 00:44:10

标签: javascript arrays

我知道这是一个noob问题,为此道歉。但我有以下格式的数据列表:

{
    "Year": 1881,
    "Jan": -8,
    "Feb": -13,
    "Mar": 2,
    "Apr": -2,
    "May": -3,
    "Jun": -27,
    "Jul": -5,
    "Aug": -1,
    "Sep": -8,
    "Oct": -18,
    "Nov": -25,
    "Dec": -14,
    "J-D": -10,
    "D-N": -11,
    "DJF": -13,
    "MAM": -1,
    "JJA": -11,
    "SON": -17
},
{
    "Year": 1882,
    "Jan": 10,
    "Feb": 10,
    "Mar": 2,
    "Apr": -19,
    "May": -17,
    "Jun": -24,
    "Jul": -9,
    "Aug": 5,
    "Sep": 0,
    "Oct": -21,
    "Nov": -20,
    "Dec": -24,
    "J-D": -9,
    "D-N": -8,
    "DJF": 2,
    "MAM": -11,
    "JJA": -9,
    "SON": -14
},

我想计算每年的平均温度。有超过100年。最简单的方法是什么?将它们放入数组中并手动计算它们需要很长时间。即便如此,我也不知道如何指定每年的平均温度。我可以用什么方式编写这段代码?我正在使用Javascript。

4 个答案:

答案 0 :(得分:3)

首先,您需要一组键来表示月份(因此您可以忽略所有其他数据)

let months = ['Jan', 'Feb', 'Mar', ...];

然后,假设您的数据位于实际数组中,您可以使用reduce operation(两个简化操作)来生成平均年份的地图



let years = [{"Year":1881,"Jan":-8,"Feb":-13,"Mar":2,"Apr":-2,"May":-3,"Jun":-27,"Jul":-5,"Aug":-1,"Sep":-8,"Oct":-18,"Nov":-25,"Dec":-14,"J-D":-10,"D-N":-11,"DJF":-13,"MAM":-1,"JJA":-11,"SON":-17},{"Year":1882,"Jan":10,"Feb":10,"Mar":2,"Apr":-19,"May":-17,"Jun":-24,"Jul":-9,"Aug":5,"Sep":0,"Oct":-21,"Nov":-20,"Dec":-24,"J-D":-9,"D-N":-8,"DJF":2,"MAM":-11,"JJA":-9,"SON":-14}];

let months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

let averages = years.reduce((avgs, year) => {
    let sum = months.reduce((avg, month) => avg + year[month], 0);
    avgs[year.Year] = sum / months.length;
    return avgs;
}, {});

document.write('<pre>' + JSON.stringify(averages, null, '  ') + '</pre>');
&#13;
&#13;
&#13;

答案 1 :(得分:2)

这是使用现代数组方法的答案,通过解决问题并将其分解为步骤,使解决方案更易于阅读。

// The data you provided
var data = [
    {
        "Year": 1881,
        "Jan": -8,
        "Feb": -13,
        "Mar": 2,
        "Apr": -2,
        "May": -3,
        "Jun": -27,
        "Jul": -5,
        "Aug": -1,
        "Sep": -8,
        "Oct": -18,
        "Nov": -25,
        "Dec": -14,
        "J-D": -10,
        "D-N": -11,
        "DJF": -13,
        "MAM": -1,
        "JJA": -11,
        "SON": -17
    },
    {
        "Year": 1882,
        "Jan": 10,
        "Feb": 10,
        "Mar": 2,
        "Apr": -19,
        "May": -17,
        "Jun": -24,
        "Jul": -9,
        "Aug": 5,
        "Sep": 0,
        "Oct": -21,
        "Nov": -20,
        "Dec": -24,
        "J-D": -9,
        "D-N": -8,
        "DJF": 2,
        "MAM": -11,
        "JJA": -9,
        "SON": -14
    },
];

// Hardcode which keys count towards the average, ignore the rest like "Year"
// and "J-D".
var months = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ');

var result = data.map(yearData => ({     // Set `result` to `data` with each
                                         // item replaced with an object with...
    Year: yearData.Year,                 // The `Year` property copied
    average: (months                     // An `average` property set to the
                                         // result of taking `months` and...
        .map(month => yearData[month])   // Replacing each item with the value
                                         // at that key in the current year data
        .reduce((a, b) => a + b)         // Then taking the sum of the Array
        / months.length                  // Then going from sum to average by
                                         // dividing by the number of items
    ),
}));

// `result` now contains the averages.

有很多方法可以解决这个问题,这不是唯一的解决方案。每种方法的共同之处在于,无论如何,您需要迭代数组(由mapreduce完成),存储总和(这里由隐式处理) reduce),然后除以月数。

答案 2 :(得分:1)

可选地

jsFiddle

&#13;
&#13;
var temps = [
{ "Year": 1881, "Jan": -8, "Feb": -13, "Mar": 2, "Apr": -2, "May": -3, "Jun": -27, "Jul": -5, "Aug": -1, "Sep": -8, "Oct": -18, "Nov": -25, "Dec": -14, "J-D": -10, "D-N": -11, "DJF": -13, "MAM": -1, "JJA": -11, "SON": -17 },
{ "Year": 1882, "Jan": 10, "Feb": 10, "Mar": 2, "Apr": -19, "May": -17, "Jun": -24, "Jul": -9, "Aug": 5, "Sep": 0, "Oct": -21, "Nov": -20, "Dec": -24, "J-D": -9, "D-N": -8, "DJF": 2, "MAM": -11, "JJA": -9, "SON": -14 }
],
total = 0;

for(var i=0, lng = temps.length; i < lng; ++i){
    var y = temps[i],
  	yearAvg = parseFloat(((y.Jan + y.Feb + y.Mar + y.Apr + y.May + y.Jun + y.Jul + y.Aug + y.Sep + y.Oct + y.Nov + y.Dec ) / 12).toFixed(2), 10);
    console.log(y.Year + ' average = ' + yearAvg);
  total += yearAvg;
}

document.getElementById('result').innerHTML = 'Total Avergae: ' + total/temps.length;
&#13;
<div id="result"></div>
&#13;
&#13;
&#13;

答案 3 :(得分:-2)

我建议编写一个python脚本来将字符串解析为多维字典。 dict的第一层应该是表示年份的键,值应该包含第二层字典,其中键是一年中的月份,值是相关的温度。从那里你可以编写一个函数来迭代dict并计算Jaromanda建议的平均值。

我会为你写出来,但这是一个值得学习的好项目。查找python多维词典以及如何解析字符串。