javascript 如何从数组的嵌套中创建嵌套数组?[关闭]

zf2sa74q  于 2024-01-05  发布在  Java
关注(0)|答案(1)|浏览(170)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
3天前关闭。
Improve this question
我希望返回一个对象数组,其中包含一个对象的子数组。
我提取的数据包含一个“projects”数组,该数组中包含一系列属性,该数组中包含一个“work items”数组。我想循环/Map"projects“数组并返回一个新的对象数组,其中将有一个”subtasks“属性,该属性需要包含另一个对象数组,该对象数组是从”projects“数组中的”work items“数组开发的。
下面是我到目前为止所做的。这不是我想要的,因为我需要在循环项目时创建主对象,然后在循环“工作项”时创建“子任务”数组。我试图将总体对象放在“return project.workItems.map...”行上方,但我得到了语法错误。
请参阅下面的代码以获得预期输出的示例。

  1. let newProjects1 = projects.map((project, index) => {
  2. return project.workItems.map((workItem) => {
  3. return {
  4. TaskID: index,
  5. ProjectNo: project.projectNumber,
  6. TaskName: project.projectTitle,
  7. StartDate: new Date("11/11/2024"),
  8. EndDate: new Date(project.projectEnd),
  9. subtasks: [
  10. {
  11. TaskID: index,
  12. TaskName: workItem.name,
  13. StartDate: new Date("11/11/2024"),
  14. Duration: 80,
  15. Progress: 150,
  16. },
  17. ],
  18. };
  19. })
  20. ;

字符串
});
最终的输出结果应该是这样的

  1. [
  2. {
  3. TaskID: 1,
  4. ProjectNo: "29895",
  5. TaskName: "Stoneybridge WTW",
  6. StartDate: new Date(projects[0].projectStart),
  7. EndDate: new Date(projects[0].projectEnd),
  8. subtasks: [
  9. {
  10. TaskID: 1.1,
  11. TaskName: "Sodium Carbonate",
  12. StartDate: new Date(projects[0].projectStart),
  13. Duration: 4,
  14. Progress: 150,
  15. comments: "unlucky",
  16. subtasks: [
  17. {
  18. TaskID: 1.11,
  19. TaskName: "Design",
  20. StartDate: new Date(projects[0].projectStart),
  21. Duration: 30,
  22. Progress: 150,
  23. },
  24. {
  25. TaskID: 1.12,
  26. TaskName: "Workshop",
  27. StartDate: new Date(projects[0].projectStart),
  28. Duration: 30,
  29. Progress: 150,
  30. },
  31. {
  32. TaskID: 1.13,
  33. TaskName: "Site",
  34. StartDate: new Date(projects[0].projectStart),
  35. Duration: 30,
  36. Progress: 150,
  37. },
  38. ],
  39. },]


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

  1. [{
  2. "projectNumber": 26278,
  3. "projectTitle": "Ifon WTW",
  4. "chemicals": ["sodium hypochlorite", "sodium hydroxide", "pacl"],
  5. "projectStatus": "site survey",
  6. "siteType": "SR",
  7. "location": "Glasgow",
  8. "contractType": "construction",
  9. "designLead": "Craig Garvie",
  10. "projectManager": "Isaac Stanton",
  11. "projectType": "Other",
  12. "spm": "Mark Tench",
  13. "client": "Yorkshire Water",
  14. "comments": "project going swimmingly",
  15. "projectStart": "12/20/2022",
  16. "projectEnd": "07/28/2024",
  17. "createdAt": "2022-11-22T07:43:42Z",
  18. "updatedAt": "2023-04-09T10:13:14Z",
  19. "equipment": [
  20. { "name": "kiosk", "count": 2 },
  21. { "name": "tanker fill point", "count": 1 },
  22. { "name": "dosing skid", "count": 1 },
  23. { "name": "POA catchpot", "count": 1 },
  24. { "name": "Low Point Catchpot", "count": 0 },
  25. { "name": "MCC", "count": 1 }
  26. ],
  27. "workItems": [
  28. {
  29. "name": "work Item 1",
  30. "siteSurveyStart": "11/29/2022",
  31. "siteSurveyEnd": "01/25/2023",
  32. "designStart": "02/14/2023",
  33. "designEnd": "03/06/2023",
  34. "workShopStart": "04/24/2023",
  35. "workShopEnd": "05/05/2023",
  36. "rsePremisesStart": "06/24/2023",
  37. "rsePremisesEnd": "07/09/2023",
  38. "siteStart": "08/20/2023",
  39. "siteEnd": "09/13/2023"
  40. },
  41. {
  42. "name": "work Item 2",
  43. "siteSurveyStart": "11/02/2022",
  44. "siteSurveyEnd": "01/05/2023",
  45. "designStart": "02/02/2023",
  46. "designEnd": "03/24/2023",
  47. "workShopStart": "04/11/2023",
  48. "workShopEnd": "05/19/2023",
  49. "rsePremisesStart": "06/19/2023",
  50. "rsePremisesEnd": "07/17/2023",
  51. "siteStart": "08/20/2023",
  52. "siteEnd": "09/23/2023"
  53. }
  54. ],
  55. "chemical": [{ "name": "sodium carbonate" }, { "name": "sulphuric acid" }],
  56. "projectPersonnel": [
  57. { "name": "daniel carey" },
  58. { "name": "erin donnelly" },
  59. { "name": "craig garvie" },
  60. { "name": "ryan watson" },
  61. { "name": "lewis scott" },
  62. { "name": "jack overfield" },
  63. { "name": "fidel hernandez" }
  64. ]


}]

wnvonmuf

wnvonmuf1#

要获得此输出,您可以使用map函数的组合来创建嵌套结构。下面是如何修改代码以实现此目的的示例:

  1. let newProjects = projects.map((project, index) => {
  2. return {
  3. TaskID: index + 1,
  4. ProjectNo: project.projectNumber,
  5. TaskName: project.projectTitle,
  6. StartDate: new Date(project.projectStart),
  7. EndDate: new Date(project.projectEnd),
  8. subtasks: project.workItems.map((workItem, subIndex) => {
  9. return {
  10. TaskID: index + 1 + '.' + (subIndex + 1),
  11. TaskName: workItem.name,
  12. StartDate: new Date(project.projectStart),
  13. Duration: 30,
  14. Progress: 150,
  15. subtasks: workItem.subtasks.map((subtask, subtaskIndex) => {
  16. return {
  17. TaskID: index + 1 + '.' + (subIndex + 1) + '.' + (subtaskIndex + 1),
  18. TaskName: subtask.name,
  19. StartDate: new Date(project.projectStart),
  20. Duration: 30,
  21. Progress: 150,
  22. };
  23. }),
  24. };
  25. }),
  26. };
  27. });

字符串
此代码Mapprojects数组并为每个项目创建一个新对象。在每个项目对象中,它再次使用map函数以workItems数组为基础创建subtasks数组。
确保将Duration和Progress替换为数据模型中的实际属性。

展开查看全部

相关问题