I am currently developing an Angular application and here is the structure of my data:
data= [
{
"id": 2,
"name": "Jamie",
"objectId": 200,
"parentId": null,
"children": [
{
"id": 98,
"name": "Rose",
"objectId": 100,
"parentId": 200,
"children": [
{
"id": 1212,
"name": "Julie",
"objectId": 500,
"parentId": 100,
"children": []
}
]
},
{
"id": 67,
"name": "Kosy",
"objectId": 700,
"parentId": 200,
"children": []
}
]
}
]
In my method, I will be receiving an input ID and name. For example, if the ID is 1212 and the name is "Julie", I need to traverse to the node where the ID is 1212 and the name is "Julie". Once this condition is met, I have to check whether the parent ID in children matches the object ID in the parent until the parent ID becomes null.
If the parent ID becomes null, it is considered as the last node, and the desired output should be an array in the following format. For ID 1212 and the name "Julie", the resultArray would be
resultArray = ["Jamie/Rose/Julie"]
, with data separated by slashes from parent to children.
Another example would be if I receive the ID as 67 and the name as "Kosy", then the result array would be
resultArray = ["Jamie/Kosy"]
Since the parent ID of Kosy is 200 and the object ID of Jamie is also 200, it indicates that Jamie is the parent of Kosy, resulting in the formatted data as mentioned above. I aim to create a dynamic code to handle large amounts of data at runtime while maintaining the same structure and logic.
How can I achieve this?