I have been trying to restructure the following flat array (participations, see below) to a more organized tree form, so that i can use Tree grid component of syncfusion. I have tried using the .reduce() function. but it seems i cannot make the correct strucutre. I have also tried lodash to group them by unique id. anyways here is what someone in this platform has helped to move forward: The Starting array participations is below. The names of some properties need to be renamed as well.
//what ive tried so far
const custommodifier = (participations) => participations.reduce((a,{KlasCode, LESDatum, LESID, Moduleomschrijving,ParticipationLetterCode}) => {
if (a[KlasCode] ){
if (a[ParticipationLetterCode] ){
a[KlasCode].subtasks[0].subtasks[0].subtasks.push({
// ParticipationLetterCode,
taskName: LESDatum,
LESID,
})
} else {
// a[KlasCode].subtasks[0].subtasks[0].taskName = ParticipationLetterCode
a[KlasCode].subtasks[0].subtasks.push({
taskName: ParticipationLetterCode,
subtasks: [{
taskName: LESDatum,
}]
})
}
} else {
a[KlasCode] = {
taskName: KlasCode,
subtasks: [{
taskName:Moduleomschrijving,
subtasks: [{
taskName: ParticipationLetterCode,
subtasks: [{
// ParticipationLetterCode,
taskName: LESDatum,
LESID,
}]
}]
}]
}
}
return a;
}, {});
Below you can find the correct data structure a custom function should make it look. Thakns anyone seeing this
//starting point
let participations = [{
KlasCode: "1S RD BJ GS ma-d",
LESDatum: "12/12/20",
LESID: "1",
ModuleID: "1050",
Moduleomschrijving:"Realisaties blouse/jurk",
ParticipationLetterCode: "X"
}, {
KlasCode: "1S RD BJ GS ma-d",
LESDatum: "11/11/20",
LESID: "2",
ModuleID: "1050",
Moduleomschrijving:"Realisaties blouse/jurk",
ParticipationLetterCode: "X",
},
{
KlasCode: "1S RD BJ GS ma-d",
LESDatum: "1/1/20",
LESID: "3",
ModuleID: "1050",
Moduleomschrijving:"Realisaties blouse/jurk",
ParticipationLetterCode: "Y"
},
{
KlasCode: "2S RD BJ RR ma-d",
LESDatum: "5/12/20",
LESID: "4",
ModuleID: "1051",
Moduleomschrijving:"Realisaties shirts",
ParticipationLetterCode: "Z"
},
{
KlasCode: "2S RD BJ RR ma-d",
LESDatum: "6/11/20,
LESID: "4",
ModuleID: "1051",
Moduleomschrijving:"Realisaties shirts",
ParticipationLetterCode: "Z"
}
]
// Need to make the data look like this including field name change:
let participations = [{
"taskName": "1S RD BJ GS ma-d",
"subtasks": [{
"ModuleID": "1050",
"taskName": "Realisaties blouse/jurk",
"subtasks": [{
"taskName": "X",
"subtasks": [{
"taskName": "12/12/20",
"LESID": "1",
},
{
"taskName": "11/11/20",
"LESID": "2",
}
],
},
{
"taskName": "Y",
"subtasks": [{
"taskName": "1/1/20",
"LESID": "3",
}]
}
]
}]
},
{
"taskName": "2S RD BJ RR ma-d",
"subtasks": [{
"ModuleID": "1051",
"taskName": "Realisaties shirts",
"subtasks": [{
"taskName": "Z",
"subtasks": [{
"taskName": "5/12/20",
"LESID":"4"
},
{
"taskName": "6/11/20",
"LESID":"5"
}
],
}
]
}]
}]