如何从数组嵌套创建嵌套数组?

问题描述 投票:0回答:1

我希望返回一个包含对象子数组的对象数组。

我从中提取的数据包含一个带有一堆属性的“项目”数组,在这个数组中有一个“工作项”数组。我想循环/映射“项目”数组并返回一个新的对象数组,其中将有一个“子任务”属性,该属性需要包含另一个从“工作项”数组开发的对象数组“项目”数组。

以下是我到目前为止所到达的地方。这不是我想要的,因为我需要在循环项目时创建主对象,然后在循环“工作项”时创建“子任务”数组。我尝试将总体对象放在“return project.workItems.map...”行上方,但出现语法错误。

请参阅下面的代码以获取预期输出的示例。

  let newProjects1 = projects.map((project, index) => {

  return project.workItems.map((workItem) => {
    return {
      TaskID: index,
      ProjectNo: project.projectNumber,
      TaskName: project.projectTitle,
      StartDate: new Date("11/11/2024"),
      EndDate: new Date(project.projectEnd),
      subtasks: [
        {
          TaskID: index,
          TaskName: workItem.name,
          StartDate: new Date("11/11/2024"),
          Duration: 80,
          Progress: 150,
        },
      ],
    };
  })
;

});

最终输出需要看起来像这样

  [
{
  TaskID: 1,
  ProjectNo: "29895",
  TaskName: "Stoneybridge WTW",
  StartDate: new Date(projects[0].projectStart),
  EndDate: new Date(projects[0].projectEnd),
  subtasks: [
    {
      TaskID: 1.1,
      TaskName: "Sodium Carbonate",
      StartDate: new Date(projects[0].projectStart),
      Duration: 4,
      Progress: 150,
      comments: "unlucky",
      subtasks: [
        {
          TaskID: 1.11,
          TaskName: "Design",
          StartDate: new Date(projects[0].projectStart),
          Duration: 30,
          Progress: 150,
        },
        {
          TaskID: 1.12,
          TaskName: "Workshop",
          StartDate: new Date(projects[0].projectStart),
          Duration: 30,
          Progress: 150,
        },
        {
          TaskID: 1.13,
          TaskName: "Site",
          StartDate: new Date(projects[0].projectStart),
          Duration: 30,
          Progress: 150,
        },
      ],
    },]

这是输入数据,即“projects”数组,其中包含“workItems”数组。这只是“projects”数组中的对象之一

 [{
"projectNumber": 26278,
"projectTitle": "Ifon WTW",
"chemicals": ["sodium hypochlorite", "sodium hydroxide", "pacl"],
"projectStatus": "site survey",
"siteType": "SR",
"location": "Glasgow",
"contractType": "construction",
"designLead": "Craig Garvie",
"projectManager": "Isaac Stanton",
"projectType": "Other",
"spm": "Mark Tench",
"client": "Yorkshire Water",
"comments": "project going swimmingly",
"projectStart": "12/20/2022",
"projectEnd": "07/28/2024",
"createdAt": "2022-11-22T07:43:42Z",
"updatedAt": "2023-04-09T10:13:14Z",
"equipment": [
  { "name": "kiosk", "count": 2 },
  { "name": "tanker fill point", "count": 1 },
  { "name": "dosing skid", "count": 1 },
  { "name": "POA catchpot", "count": 1 },
  { "name": "Low Point Catchpot", "count": 0 },
  { "name": "MCC", "count": 1 }
],
"workItems": [
  {
    "name": "work Item 1",
    "siteSurveyStart": "11/29/2022",
    "siteSurveyEnd": "01/25/2023",
    "designStart": "02/14/2023",
    "designEnd": "03/06/2023",
    "workShopStart": "04/24/2023",
    "workShopEnd": "05/05/2023",
    "rsePremisesStart": "06/24/2023",
    "rsePremisesEnd": "07/09/2023",
    "siteStart": "08/20/2023",
    "siteEnd": "09/13/2023"
  },
  {
    "name": "work Item 2",
    "siteSurveyStart": "11/02/2022",
    "siteSurveyEnd": "01/05/2023",
    "designStart": "02/02/2023",
    "designEnd": "03/24/2023",
    "workShopStart": "04/11/2023",
    "workShopEnd": "05/19/2023",
    "rsePremisesStart": "06/19/2023",
    "rsePremisesEnd": "07/17/2023",
    "siteStart": "08/20/2023",
    "siteEnd": "09/23/2023"
  }
],
"chemical": [{ "name": "sodium carbonate" }, { "name": "sulphuric acid" }],
"projectPersonnel": [
  { "name": "daniel carey" },
  { "name": "erin donnelly" },
  { "name": "craig garvie" },
  { "name": "ryan watson" },
  { "name": "lewis scott" },
  { "name": "jack overfield" },
  { "name": "fidel hernandez" }
]

}]

javascript arrays loops javascript-objects
1个回答
0
投票

要获得此输出,您可以使用

map
函数的组合来创建嵌套结构。以下是如何修改代码以实现此目的的示例:

let newProjects = projects.map((project, index) => {
    return {
        TaskID: index + 1,
        ProjectNo: project.projectNumber,
        TaskName: project.projectTitle,
        StartDate: new Date(project.projectStart),
        EndDate: new Date(project.projectEnd),
        subtasks: project.workItems.map((workItem, subIndex) => {
            return {
                TaskID: index + 1 + '.' + (subIndex + 1),
                TaskName: workItem.name,
                StartDate: new Date(project.projectStart),
                Duration: 30,
                Progress: 150,
                subtasks: workItem.subtasks.map((subtask, subtaskIndex) => {
                    return {
                        TaskID: index + 1 + '.' + (subIndex + 1) + '.' + (subtaskIndex + 1),
                        TaskName: subtask.name,
                        StartDate: new Date(project.projectStart),
                        Duration: 30,
                        Progress: 150,
                    };
                }),
            };
        }),
    };
});

此代码映射

projects
数组并为每个项目创建一个新对象。在每个项目对象中,它再次使用
map
函数基于
subtasks
数组创建
workItems
数组。

确保将 Duration 和 Progress 替换为数据模型中的实际属性。

© www.soinside.com 2019 - 2024. All rights reserved.