I’m looking to return an array of objects which contains a sub array of objects.
The data I am extracting from contains an array of ‘projects’ with a bunch of properties and within this array there is an array of ‘work items’. I want to loop/map through the ‘projects’ array and return a new array of objects and within that there will be a ‘subtasks’ property which needs to contain another array of objects which has been developed from the ‘work items’ array within the ‘projects’ array.
Below is where I have got to so far. This is not what I want as I need the main object to just be created when it loops through the projects and then the ‘subtasks’ array to be created when it loops through the ‘work items’. I tried putting the overarching object above the ‘return project.workItems.map….’ line but I was getting syntax errors.
See below this code for an example of the expected output.
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,
},
],
};
})
;
});
The final output needs to look something like this
[
{
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,
},
],
},]
This is the input data i.e. the ‘projects’ array, which contains the ‘workItems’ array. This is just one of the objects in the ‘projects’ array
[{
"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" }
]
}]