Last active
February 25, 2021 18:31
-
-
Save RicardoMurad/329a129dbc531ca2407777932448af68 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| postgres=# select * from employees; | |
| id | name | parent_id | |
| ----+----------------------+----------- | |
| 1 | CEO | | |
| 2 | CTO | 1 | |
| 3 | CIO | 1 | |
| 4 | CFO | 1 | |
| 5 | Engineering Manager | 2 | |
| 6 | Finance Director | 4 | |
| 7 | Information Security | 3 | |
| 8 | Tech Lead | 5 | |
| */ | |
| // Input | |
| [ | |
| { | |
| "id": 1, | |
| "name": "CEO", | |
| "parent": null | |
| }, | |
| { | |
| "id": 2, | |
| "name": "CFO", | |
| "parent": 1 | |
| }, | |
| { | |
| "id": 3, | |
| "name": "CTO", | |
| "parent": 1 | |
| }, | |
| { | |
| "id": 4, | |
| "name": "Finance Manager 1", | |
| "parent": 2 | |
| }, | |
| { | |
| "id": 5, | |
| "name": "Finance Manager 2", | |
| "parent": 2 | |
| }, | |
| { | |
| "id": 6, | |
| "name": "Tech Manager 1", | |
| "parent": 3 | |
| }, | |
| { | |
| "id": 7, | |
| "name": "Tech Manager 2", | |
| "parent": 3 | |
| } | |
| ] | |
| // Output | |
| { | |
| "id": 1, | |
| "name": "CEO", | |
| "children": [ | |
| { | |
| "id": 2, | |
| "name": "CFO", | |
| "children": [ | |
| { | |
| "id": 4, | |
| "name": "Finance Manager 1", | |
| "children": [] | |
| }, | |
| { | |
| "id": 5, | |
| "name": "Finance Manager 2", | |
| "children": [] | |
| } | |
| ] | |
| }, | |
| { | |
| "id": 3, | |
| "name": "CTO", | |
| "children": [ | |
| { | |
| "id": 6, | |
| "name": "Tech Manager 1", | |
| "children": [] | |
| }, | |
| { | |
| "id": 7, | |
| "name": "Tech Manager 2", | |
| "children": [] | |
| } | |
| ] | |
| }, | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment