**已关闭。**此问题需要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...”行上方,但我得到了语法错误。
请参阅下面的代码以获得预期输出的示例。
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" }
]
型
}]
1条答案
按热度按时间wnvonmuf1#
要获得此输出,您可以使用
map
函数的组合来创建嵌套结构。下面是如何修改代码以实现此目的的示例:字符串
此代码Map
projects
数组并为每个项目创建一个新对象。在每个项目对象中,它再次使用map
函数以workItems
数组为基础创建subtasks
数组。确保将Duration和Progress替换为数据模型中的实际属性。