出于某种原因,我在使用 Javascript 数组获得所需结果时遇到了困难。
我有几行数据,其中包含日期,每行有几个时间戳。我需要做的是将该数据转换为 chart.js 的数组,但按周合并行,并对小时数(从时间戳记)求和。下面是我根据查询结果创建新数组的地方,但我正在努力按照我的需要获取它。
这是我创建数组的地方(经过处理):
function chartData(queryrowlist){
var weeks = [];
angular.forEach(queryrowlist, function(value, key)
{
weeks.push(moment(value.date).format('w'),getOnHours(value),getOffHours(value))
});
// create chart array ...
return chart;
}
weeks 数组的结尾是这样的:
["23",28800000,3600000,"23",30300000,2700000,"24",35400000,3300000,"24",30000000,3300000]
这是周数,OnHours,OffHours
周数将用作标签,小时数将用作 angular-chart/chart.js 中的开/关数据。问题是所有周数都必须是唯一的,并且每周汇总所有小时数。
所以,我需要输出这样一个数组:
chart['weeks']=["23","24"]
chart['hours']=[[59100000,6300000],[65400000,6300000]]
这可能很简单,只是我碰壁了……但如果有人有解决方案,我将不胜感激。
提前谢谢你。
更新:
对于任何可能提到这个的人来说,结果数组对于 chart.js 是不正确的。下面的答案是正确的......正如所问的那样,我只是问错了事情,然后才意识到。感觉像个傻瓜:/
Chart.js 需要同一数组中的所有行值,所以上面的示例不是:
chart['weeks']=["23","24"]
chart['hours']=[[onhours,offhours],[onhours,offhours]]
应该是:
chart['weeks']=["23","24"]
chart['hours']=[[onhours,onhours],[offhours,offhours]]
更新 2
添加进一步说明:
对于每个标签,都应该有一个值。如果你有 3 个标签,你应该有 3 个值。在我的例子中,有 2 组值数组,那是因为我有 2 个系列(2 行),1 个用于上类时间,1 个用于下类时间。所以这是一个完整的数组列表。
chart['labels']=["23","24","25"] // strings
chart['data']=[[onhours,onhours,onhours],[offhours,offhours,offhours]] // integers
chart['series']=['On Hours','Off Hours'] // strings
如果我只需要 1 行:
chart['labels']=["23","24","25","26"] // strings
chart['data']=[[onhours,onhours,onhours,onhours]] // integers, notice [ [ ] ]
chart['series']=['On Hours'] // strings
希望对您有所帮助!
最佳答案
为什么不像你想要的那样拆分成两个数组?
function chartData(queryrowlist){
// Set up our data store + return object
var chart = {
weeks: [],
hours: []
};
// For every value in the parameter given
angular.forEach(queryrowlist, function(value, key) {
// Get the week, and check if it is in the array
var week = moment(value.date).format('w');
var weekIndex = chart.weeks.indexOf(week);
// If the index is -1, it is not in the array, so just push values;
if(weekIndex === -1) {
chart.weeks.push(week);
chart.hours.push([getOnHours(value),getOffHours(value)]);
} else {
// If the index is not, we assume that the weekIndex is the correct index to sum the values on.
chart.hours[weekIndex] = [chart.hours[weekIndex][0] + getOnHours(value), chart.hours[weekIndex][1] + getOffHours(value)];
}
});
return chart;
}
编辑:反射(reflect)对问题的编辑的新答案。
算法基本保持不变,但输出数组会略有变化。
function chartData(queryrowlist){
// Set up our data store + return object
var chart = {
weeks: [],
// Notice, two arrays in array
hours: [
[],
[]
]
};
// For every value in the parameter given
angular.forEach(queryrowlist, function(value, key) {
// Get the week, and check if it is in the array
var week = moment(value.date).format('w');
var weekIndex = chart.weeks.indexOf(week);
// If the index is -1, it is not in the array, so just push values;
if(weekIndex === -1) {
chart.weeks.push(week);
chart.hours[0].push(getOnHours(value));
chart.hours[1].push(getOffHours(value));
} else {
// If the index is not, we assume that the weekIndex is the correct index to sum the values on.
// Also notice the change to output to different arrays
chart.hours[0][weekIndex] += getOnHours(value);
chart.hours[1][weekIndex] += getOffHours(value);
}
});
return chart;
}
关于javascript - 使用 Unique 和 Sum 从现有数组值创建新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37950285/