jquery 操纵Array对象

vwkv1x7d  于 2023-10-17  发布在  jQuery
关注(0)|答案(1)|浏览(77)

我有波纹管数组对象

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;
    }
  });
});
6vl6ewon

6vl6ewon1#

我有你要找的东西。如果你使用ES6,它可以简单地完成。

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 project = {
   "Project 1": "0",
   "Project 2": "0",
   "Project 3": "0",
   "Project 4": "0"
 };
 const newDataSource = datasource.reduce((res, current) => {
    return [
      ...res, 
      {
        "Region": current.Region, 
        "YearMonth": current.YearMonth, 
        ...project,
        [current.projectname]: current.Total_Time_Spent
      }
    ];
 }, []);
 
 console.log(newDataSource)

相关问题