I have a task in which I need to convert an Array of Objects data to specified data tree structure for to display it in frontend tree-table. This below is my Array of Objects data.
<!-- This is my JSON Data -->
Collection: {
Fields: [{
fieldName: 'mainField',
properties: [{
id: 1,
carName: 'carName_01',
type:string'
},
{
id: 2,
carName: 'carName_02',
type: 'object',
ref: 'subField'
}]
},
{
fieldName: 'subField',
properties: [{
id: 1,
carName: 'carName_11',
type: 'string'
},
{
id: 1,
carName: 'carName_12',
type: 'object',
ref: 'subField1'
}]
},
{
fieldName: 'subField1',
properties: [{
id: 1,
carName: 'carName_21',
type: 'string'
}]
}]
}
I have to change this data into this below following data structure.
<!-- Desired Output -->
tableData: [{
id: 1,
carName: 'carName_01',
type: 'string'
},
{
id: 2,
carName: 'carName_02',
type: 'object'
source: 'subField'
children: [{
id: 11,
carName: 'carName_11',
type: 'object',
ref: 'subField1',
children: [{
id: 12,
carName: 'carName_12',
type: 'string'
}]
}
I have tried to do this using Recursion in methods but, I can't do that in a way that I want. My task is I have to add an children whenever the type: 'object' is object with another referenced object inside that array of objects. And by the way the data is dyanmic which is from mongodb database. That's just a simplified version of the code.
If my information is not enough or seemed unreasonable sorry for that, I am fresher so, I just asked what i want. Thank you in advance for the help.