Skip to content

Instantly share code, notes, and snippets.

@giorgiberia
Last active May 29, 2020 15:20
Show Gist options
  • Save giorgiberia/4be7bd89c533b56853846e3596511605 to your computer and use it in GitHub Desktop.
Save giorgiberia/4be7bd89c533b56853846e3596511605 to your computer and use it in GitHub Desktop.
parent child task sub tasks flat array to nested by children
def buildTree(tasks, parentId=0):
branch = []
for task in tasks:
if task['parent_id'] == parentId:
children = buildTree(tasks, task['id'])
if children:
# print(list(children.keys()))
task['children'] = children
else:
task['children'] = []
task['text'] = task['name']
branch.append(task)
return branch
@giorgiberia
Copy link
Author

giorgiberia commented May 29, 2020

produces nested array of parent child objects

{
  "code": 1,
  "msg": "success",
  "result": [
    {
      "id": 1,
      "name": "test",
      "description": "test",
      "assignee": 1,
      "parent_id": 0,
      "projectId": 1,
      "unitId": null,
      "volume": null,
      "deviation": null,
      "children": [
        {
          "id": 2,
          "name": "test2",
          "description": "test",
          "assignee": 1,
          "parent_id": 1,
          "projectId": 1,
          "unitId": null,
          "volume": null,
          "deviation": null,
          "children": [
            {
              "id": 3,
              "name": "test3",
              "description": "etyst\r\n",
              "assignee": 1,
              "parent_id": 2,
              "projectId": 1,
              "unitId": null,
              "volume": null,
              "deviation": null,
              "children": [
                {
                  "id": 4,
                  "name": "test4",
                  "description": null,
                  "assignee": 1,
                  "parent_id": 3,
                  "projectId": 1,
                  "unitId": null,
                  "volume": null,
                  "deviation": null,
                  "children": [
                    
                  ],
                  "text": "test4"
                }
              ],
              "text": "test3"
            },
            {
              "id": 5,
              "name": "test5",
              "description": "etyst\r\n",
              "assignee": 1,
              "parent_id": 2,
              "projectId": 1,
              "unitId": null,
              "volume": null,
              "deviation": null,
              "children": [
                
              ],
              "text": "test5"
            }
          ],
          "text": "test2"
        }
      ],
      "text": "test"
    },
    {
      "id": 6,
      "name": "test6",
      "description": "test6\r\n",
      "assignee": 1,
      "parent_id": 0,
      "projectId": 1,
      "unitId": null,
      "volume": null,
      "deviation": null,
      "children": [
        
      ],
      "text": "test6"
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment