JS表达式计算一个月内所有给定数据点的平均值,对于数据点中表示的所有月份
JS expression to calculate the average value of all given data points within a month, for all months represented in the data points
我正在寻找一个 JavaScript 表达式来计算一个月内所有给定数据点的平均值,对于数据点中表示的所有月份。到目前为止,我只考虑过为每个月硬编码新数组并用 12 个条件向它们添加,然后对每个列表的值进行平均。但我觉得肯定有一种更清洁更有效的方法。
示例数据 =(例如 [YYYY、MM、DD、值]):
[
[ 2020, 1, 2, 1058 ],
[ 2020, 1, 8, 1055 ],
[ 2020, 1, 12, 1058 ],
[ 2020, 1, 23, 1049 ],
[ 2020, 1, 24, 1050 ],
[ 2020, 1, 29, 1057 ],
[ 2020, 2, 1, 1088 ],
[ 2020, 2, 5, 1087 ],
[ 2020, 2, 13, 1101 ],
[ 2020, 2, 26, 1108 ],
[ 2020, 4, 25, 1119 ],
[ 2020, 8, 24, 1178 ],
[ 2020, 8, 25, 1196 ],
[ 2020, 9, 29, 1214 ],
[ 2020, 9, 31, 1230 ],
[ 2020, 10, 20, 1259 ],
[ 2020, 11, 18, 1276 ]
]
前三个字段是日期(YYYY, MM, DD) jan-dec表示为0-11,最后一个字段是要取平均值的值。
我想从表达式中得到的 return 将是每月平均值的数组(例如 {month: average value} ):
[{1: avg}, {2: avg}, {4: avg}, {8: avg}, {9: avg}, {10: avg}, {11: avg}]]
let arr = [
[ 2020, 1, 2, 1058 ],
[ 2020, 1, 8, 1055 ],
[ 2020, 1, 12, 1058 ],
[ 2020, 1, 23, 1049 ],
[ 2020, 1, 24, 1050 ],
[ 2020, 1, 29, 1057 ],
[ 2020, 2, 1, 1088 ],
[ 2020, 2, 5, 1087 ],
[ 2020, 2, 13, 1101 ],
[ 2020, 2, 26, 1108 ],
[ 2020, 4, 25, 1119 ],
[ 2020, 8, 24, 1178 ],
[ 2020, 8, 25, 1196 ],
[ 2020, 9, 29, 1214 ],
[ 2020, 9, 31, 1230 ],
[ 2020, 10, 20, 1259 ],
[ 2020, 11, 18, 1276 ]
];
let result = [];
let result2 = arr.reduce((res, [year, month, day, value]) => {
if (!res[month]) {
res[month] = { month: month, avg: 0, count: 0, sum: 0 };
result.push(res[month])//remove this if you are satisfied with the result in result2;
}
res[month].sum += value;
res[month].count +=1;
res[month].avg = res[month].sum/res[month].count;
return res;
} , {});
console.log('array of objects {[month, sum, count, avg]}', result);
console.log('object with values {month: {month, sum, count, avg}}', result2);
只需选择最适合您的结果即可!
给你,我的男人,它也许可以做得更干净,但如果你想要更多的东西,我会在早上 6 点之前回复你!干杯!
George 的解决方案没有问题,但这里有另一种方法可以通过大量评论实现您想要的效果。
let source = [
[ 2020, 1, 2, 1058 ],
[ 2020, 1, 8, 1055 ],
[ 2020, 1, 12, 1058 ],
[ 2020, 1, 23, 1049 ],
[ 2020, 1, 24, 1050 ],
[ 2020, 1, 29, 1057 ],
[ 2020, 2, 1, 1088 ],
[ 2020, 2, 5, 1087 ],
[ 2020, 2, 13, 1101 ],
[ 2020, 2, 26, 1108 ],
[ 2020, 4, 25, 1119 ],
[ 2020, 8, 24, 1178 ],
[ 2020, 8, 25, 1196 ],
[ 2020, 9, 29, 1214 ],
[ 2020, 9, 31, 1230 ],
[ 2020, 10, 20, 1259 ],
[ 2020, 11, 18, 1276 ]
];
// Object to store our output
let averages = {};
// Loop through the source data
source.forEach(row => {
// Create an array as this month's value if not set
if (!averages[row[1]])
averages[row[1]] = [];
// Lump the same-month values into the correct array. Use parseInt to avoid
// potential NaN errors
averages[row[1]].push(parseInt(row[3], 10));
});
// Calculate the averages by looping through each key in the object (each month),
// using reduce to get the sum of the array and then use the length property to
// get the average of that array.
Object.keys(averages).forEach(
(m) => averages[m] = averages[m].reduce((v, i) => v + i, 0) / averages[m].length
);
// TADA!
console.log(averages);
我正在寻找一个 JavaScript 表达式来计算一个月内所有给定数据点的平均值,对于数据点中表示的所有月份。到目前为止,我只考虑过为每个月硬编码新数组并用 12 个条件向它们添加,然后对每个列表的值进行平均。但我觉得肯定有一种更清洁更有效的方法。 示例数据 =(例如 [YYYY、MM、DD、值]):
[
[ 2020, 1, 2, 1058 ],
[ 2020, 1, 8, 1055 ],
[ 2020, 1, 12, 1058 ],
[ 2020, 1, 23, 1049 ],
[ 2020, 1, 24, 1050 ],
[ 2020, 1, 29, 1057 ],
[ 2020, 2, 1, 1088 ],
[ 2020, 2, 5, 1087 ],
[ 2020, 2, 13, 1101 ],
[ 2020, 2, 26, 1108 ],
[ 2020, 4, 25, 1119 ],
[ 2020, 8, 24, 1178 ],
[ 2020, 8, 25, 1196 ],
[ 2020, 9, 29, 1214 ],
[ 2020, 9, 31, 1230 ],
[ 2020, 10, 20, 1259 ],
[ 2020, 11, 18, 1276 ]
]
前三个字段是日期(YYYY, MM, DD) jan-dec表示为0-11,最后一个字段是要取平均值的值。
我想从表达式中得到的 return 将是每月平均值的数组(例如 {month: average value} ):
[{1: avg}, {2: avg}, {4: avg}, {8: avg}, {9: avg}, {10: avg}, {11: avg}]]
let arr = [
[ 2020, 1, 2, 1058 ],
[ 2020, 1, 8, 1055 ],
[ 2020, 1, 12, 1058 ],
[ 2020, 1, 23, 1049 ],
[ 2020, 1, 24, 1050 ],
[ 2020, 1, 29, 1057 ],
[ 2020, 2, 1, 1088 ],
[ 2020, 2, 5, 1087 ],
[ 2020, 2, 13, 1101 ],
[ 2020, 2, 26, 1108 ],
[ 2020, 4, 25, 1119 ],
[ 2020, 8, 24, 1178 ],
[ 2020, 8, 25, 1196 ],
[ 2020, 9, 29, 1214 ],
[ 2020, 9, 31, 1230 ],
[ 2020, 10, 20, 1259 ],
[ 2020, 11, 18, 1276 ]
];
let result = [];
let result2 = arr.reduce((res, [year, month, day, value]) => {
if (!res[month]) {
res[month] = { month: month, avg: 0, count: 0, sum: 0 };
result.push(res[month])//remove this if you are satisfied with the result in result2;
}
res[month].sum += value;
res[month].count +=1;
res[month].avg = res[month].sum/res[month].count;
return res;
} , {});
console.log('array of objects {[month, sum, count, avg]}', result);
console.log('object with values {month: {month, sum, count, avg}}', result2);
只需选择最适合您的结果即可!
给你,我的男人,它也许可以做得更干净,但如果你想要更多的东西,我会在早上 6 点之前回复你!干杯!
George 的解决方案没有问题,但这里有另一种方法可以通过大量评论实现您想要的效果。
let source = [
[ 2020, 1, 2, 1058 ],
[ 2020, 1, 8, 1055 ],
[ 2020, 1, 12, 1058 ],
[ 2020, 1, 23, 1049 ],
[ 2020, 1, 24, 1050 ],
[ 2020, 1, 29, 1057 ],
[ 2020, 2, 1, 1088 ],
[ 2020, 2, 5, 1087 ],
[ 2020, 2, 13, 1101 ],
[ 2020, 2, 26, 1108 ],
[ 2020, 4, 25, 1119 ],
[ 2020, 8, 24, 1178 ],
[ 2020, 8, 25, 1196 ],
[ 2020, 9, 29, 1214 ],
[ 2020, 9, 31, 1230 ],
[ 2020, 10, 20, 1259 ],
[ 2020, 11, 18, 1276 ]
];
// Object to store our output
let averages = {};
// Loop through the source data
source.forEach(row => {
// Create an array as this month's value if not set
if (!averages[row[1]])
averages[row[1]] = [];
// Lump the same-month values into the correct array. Use parseInt to avoid
// potential NaN errors
averages[row[1]].push(parseInt(row[3], 10));
});
// Calculate the averages by looping through each key in the object (each month),
// using reduce to get the sum of the array and then use the length property to
// get the average of that array.
Object.keys(averages).forEach(
(m) => averages[m] = averages[m].reduce((v, i) => v + i, 0) / averages[m].length
);
// TADA!
console.log(averages);