Here we have two main properties, "ID" and "FatherOrganizationID". We need to make a tree where if fatherOranizationId === null -> must be at the top, and if the fatherOrganizationId !== null, it must be in subOrganizations array of items that have ID === FatherOrganizationID. Can someone help with it? Must work in any depth. I know that it is able with recursion but have no mind how to realize it
START DATA:
{
"id": 1000,
"organizationName": "testOrg1",
"fatherOrganizationId": null,
"subOrganizations": [],
"isActive": false,
},
{
"id": 1001,
"organizationName": "testOrg2",
"fatherOrganizationId": 1000,
"subOrganizations": [],
"isActive": false,
},
{
"id": 1002,
"organizationName": "testOrg3",
"fatherOrganizationId": 1001,
"subOrganizations": [],
"isActive": false,
},
{
"id": 1003,
"organizationName": "testOrg4",
"fatherOrganizationId": 1001,
"subOrganizations": [],
"isActive": false,
},
{
"id": 1004,
"organizationName": "testOrg7",
"fatherOrganizationId": 1001,
"subOrganizations": [],
"isActive": false,
},
{
"id": 1005,
"organizationName": "testOrg7",
"fatherOrganizationId": 1002,
"subOrganizations": [],
"isActive": false,
},
{
"id": 1006,
"organizationName": "testOrg8",
"fatherOrganizationId": null,
"subOrganizations": [],
"isActive": false,
}
EXPECTED RESULT:
[
{
"id": 1000,
"organizationName": "testOrg1",
"fatherOrganizationId": null,
"subOrganizations": [
{
"id": 1001,
"organizationName": "testOrg2",
"fatherOrganizationId": 1000,
"subOrganizations": [
{
"id": 1002,
"organizationName": "testOrg3",
"fatherOrganizationId": 1001,
"subOrganizations": [
{
"id": 1005,
"organizationName": "testOrg7",
"fatherOrganizationId": 1002,
"subOrganizations": [],
"isActive": false,
}
],
"isActive": false,
},
{
"id": 1003,
"organizationName": "testOrg4",
"fatherOrganizationId": 1001,
"subOrganizations": [],
"isActive": false,
},
{
"id": 1004,
"organizationName": "testOrg7",
"fatherOrganizationId": 1001,
"subOrganizations": [],
"isActive": false,
},
],
"isActive": false,
},
],
"isActive": false,
},
{
"id": 1006,
"organizationName": "testOrg8",
"fatherOrganizationId": null,
"subOrganizations": [],
"isActive": false,
}
]