I am currently troubleshooting a recursive function, but I am struggling to identify the issue in my code.
The structure of my recursive function is as follows:
public findParent(parentId: number, node: any): any {
if (node !== undefined && node.id === parentId) {
return node;
} else {
if (node.predicates && node.predicates.length > 0) {
for (const element in node.predicates) {
if (node.predicates[element].predicates !== undefined && node.predicates[element].predicates.length > 0) {
return this.findParent(parentId, node.predicates[element]);
} else {
continue;
}
}
}
}
}
The structure of the node object is as follows:
"node": [
{
"id": 0,
"level": 0,
"field": "",
"op": "AND",
"parentId": null,
"predicates": [
{
"field": "",
"predicates": [],
"level": 1,
"parentId": 0,
"id": 1.1,
"op": ""
},
{
...
}
]
}
]
My expectation is that when I provide a parent Id to the function, it should return the complete object along with its sub/child objects.
For example, if the parentId is set to 2.5,
{
"field": "",
"predicates": [
{
"field": "",
"predicates": [],
"level": 3,
"parentId": 2.5,
"id": 3.7
},
...
],
...
},
should be returned.
However, at the moment, this function is not functioning as expected