I am looking to extract the ID values from the previous object and construct an array of IDs within each parent item. The process must be generic and should not rely on the property names. However, all properties inherit a base class called SubResource
. Only arrays that inherit from the SubResource
class should be included in the identifierHierarchy.
export abstract class SubResource {
public id: number;
public identifierHierarchy: number[] = [];
}
Consider the following data snippet:
let data = [{
"id": "1",
"name": "Deer, spotted",
"parents": [
{
"id": "133",
"name": "Jaime Coldrick",
"children": [
{
"id": "0723",
"name": "Ardys Kurten",
"grandchildren": [
{
"id": "384",
"name": "Madelle Bauman"
},
{
"id": "0576",
"name": "Pincas Maas"
},
{
"id": "5",
"name": "Corrie Beacock"
}
]
}]
}]
}]
The desired outcome is for the objects' values to be as follows:
[{
"id": "1",
"name": "Deer, spotted",
"parents": [{
"id": "133",
"name": "Jaime Coldrick",
"identifierHierarchy": ["1"],
"children": [{
"id": "0723",
"name": "Ardys Kurten",
"identifierHierarchy": ["1", "133"],
"grandchildren": [{
"id": "384",
"name": "Madelle Bauman",
"identifierHierarchy": ["1", "133", "0723"]
},
{
"id": "0576",
"name": "Pincas Maas",
"identifierHierarchy": ["1", "133", "0723"]
},
{
"id": "5",
"name": "Corrie Beacock",
"identifierHierarchy": ["1", "133", "0723"]
}
]
}]
}]
}]