【问题标题】:Convert javascript object from one form to another将 javascript 对象从一种形式转换为另一种形式
【发布时间】:2020-10-12 11:35:29
【问题描述】:

我有一个团队对象列表,其中包含团队名称、报告者和该团队每天的状态列表,我想将其转换为基于日期的对象列表,其中包含所有团队及其各自的状态日期。

我尝试过类似的方法,但没有给出预期的结果。

teams.map(team => ({
    day: team.statuses.reduce((acc, it) => it.day),
    teams: {
        teamName: team.teamName
    }
}))



 [
   {
      "teamName":"abc",
      "reportedBy": "user1",
      "statuses":[
         {
            "day":"10/12",
            "status":"green"
         },
         {
            "day":"10/11",
            "status":"green"
         },
         {
            "day":"10/09",
            "status":"green"
         }
      ]
   },
    {
      "teamName":"xyz",
      "reportedBy": "user2",
      "statuses":[
         {
            "day":"10/12",
            "status":"red"
         },
         {
            "day":"10/11",
            "status":"red"
         },
         {
            "day":"10/09",
            "status":"red"
         }
      ]
   }

]

预期输出:

[
  {
    "day": "1012",
    "teams": [
      {
        "teamName": "abc",
        "reportedBy": "user1",
        "status": "green"
      },
      {
        "teamName": "xyz",
        "reportedBy": "user2",
        "status": "red"
      }
    ]
  },
  {
    "day": "1011",
    "teams": [
      {
        "teamName": "abc",
        "reportedBy": "user1",
        "status": "green"
      },
      {
        "teamName": "xyz",
        "reportedBy": "user2",
        "status": "red"
      }
    ]
  },
  {
    "day": "1009",
    "teams": [
      {
        "teamName": "abc",
        "reportedBy": "user1",
        "status": "green"
      },
      {
        "teamName": "xyz",
        "reportedBy": "user2",
        "status": "red"
      }
    ]
  }
]

【问题讨论】:

    标签: javascript arrays json mapreduce


    【解决方案1】:

    我不认为 reduce 是这里的最佳选择。我认为最好创建一个空数组并随时填充:

    let input = [
       {
          "teamName":"abc",
          "reportedBy": "user1",
          "statuses":[
             {
                "day":"10/12",
                "status":"green"
             },
             {
                "day":"10/11",
                "status":"green"
             },
             {
                "day":"10/09",
                "status":"green"
             }
          ]
       },
        {
          "teamName":"xyz",
          "reportedBy": "user2",
          "statuses":[
             {
                "day":"10/12",
                "status":"red"
             },
             {
                "day":"10/11",
                "status":"red"
             },
             {
                "day":"10/09",
                "status":"red"
             }
          ]
       }
    ];
    
    let output = [];
    
    input.forEach(team =>
    {
      let name = team.teamName;
      let reporter = team.reporter
      team.statuses.forEach(status =>
      {
        let dayItem = output.find(item => item.day == status.day);
        if(dayItem == null)
        {
          let newItem = {day: status.day, teams:[{status: status.status, reportedBy: reporter, teamName: name}]};
          output.push(newItem);
        }
        else
        {
          dayItem.teams.push({status: status.status, reportedBy: reporter, teamName: name})
        }
      });
    });
    
    console.log(JSON.stringify(output));

    【讨论】:

    • 是的...我尝试过类似的方法,请在下面查看我的答案,但我只是想知道是否有更好的方法
    • 你如何定义更好?
    【解决方案2】:

    我尝试过类似以下的方法。

      let dayIndexed = {};
      let dayStatuses = [];
    
      teams.forEach(function(team) {
            team.statuses.forEach(function(status) {
                status.day = status.day.replace(/\//g, '');
                if(!dayIndexed[status.day]) {
                    dayIndexed[status.day] = {
                        day: status.day,
                        teams: []
                    };
                    dayStatuses.push(dayIndexed[status.day]);
                }
        
                dayIndexed[status.day].teams.push({
                    teamName: team.teamName,
                    reportedBy: team.reportedBy,
                    status: status.status   
                });
            });
        });
    

    【讨论】:

      猜你喜欢
      • 2015-01-26
      • 2015-05-11
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      相关资源
      最近更新 更多