我有波纹管数组对象
var datasource = [
{"Region": "America",
"Total_Time_Spent": "10",
"YearMonth": "2023 - October",
"projectname":"Project 1"},
{"Region": "America",
"Total_Time_Spent": "20",
"YearMonth": "2023 - October",
"projectname":"Project 2"},
{"Region": "America",
"Total_Time_Spent": "30",
"YearMonth": "2023 - June",
"projectname":"Project 3"},
{"Region": "Asia",
"Total_Time_Spent": "30",
"YearMonth": "2023 - June",
"projectname":"Project 4"}
]
基于上述我试图创建波纹管阵列格式
var Finaldatasource = [
{"Region": "America",
"YearMonth": "2023 - October",
"Project 1": "10",
"Project 2": "20",
"Project 3": "0",
"Project 4": "0"},
{"Region": "Asia",
"YearMonth": "2023 - October",
"Project 1": "0",
"Project 2": "0",
"Project 3": "0",
"Project 4": "0"}
{"Region": "America",
"YearMonth": "2023 - June",
"Project 1": "0",
"Project 2": "0",
"Project 3": "30",
"Project 4": "0"},
{"Region": "Asia",
"YearMonth": "2023 - June",
"Project 1": "0",
"Project 2": "0",
"Project 3": "0",
"Project 4": "30"}
]
表示前两列变为Region和YearMonth,后面是项目名称作为列,值为花费的小时数
我已经写了波纹管的代码和即时通讯能够获得区域和项目名称作为列和价值将花费的时间。但如何也可以包括年月列
var Finaldatasource = [];
datasource.forEach(entry => {
let existingRegion = result.find(Region => Region.Region === entry.Region);
if (existingRegion) {
existingRegion[entry.projectname] = entry.Total_Time_Spent;
} else {
let newRegion = { "Region": entry.Region };
newRegion[entry.projectname] = entry.Total_Time_Spent;
result.push(newRegion);
}
});
// Add missing months and set their values to 0
var allMonths = Array.from(new Set(dataarray.map(entry => entry.projectname)));
result.forEach(Region => {
allMonths.forEach(month => {
if (!Region.hasOwnProperty(month)) {
Region[month] = 0;
}
});
});
1条答案
按热度按时间6vl6ewon1#
我有你要找的东西。如果你使用ES6,它可以简单地完成。