Как пробежаться по дереву объекта и создать папки и файлы

Пытаюсь создать папки с файлами с бесконечной вложенностью из объекта, не пойму как это реализовать.

Нужно чтобы имена папок определялись следующим образом:

если в объекте label = null то имя папки назначить значение из order.type

в каждой вложенности должна быть папка media где по именам буду находить из другой папки файлы и копировать их в папку media, если же тип файла link то надо будет создать в папке media текстовый документ и прописать их

вложенности должны определяться по ключу children

вот дерево:

[
    {
        "id": 1,
        "label": null,
        "parentId":null,
        "media": [
            {
                "id": 1,
                "mediaType": "image",
                "title": "sl3-e0f7.jfif",
            },
            {
                "id": 2,
                "mediaType": "image",
                "title": "sl2-eaa6.jpg",
            },
            {
                "id": 3,
                "mediaType": "image",
                "title": "sl1-d942.jpg",
            },
            {
                "id": 4,
                "mediaType": "link",
                "title": "sgdfsg",
            },
            {
                "id": 5,
                "mediaType": "link",
                "title": "https://drive.google.com/file/d/1rFBqrJp4QyrWVkrDV0IYe_dEUU0sYFEl/view?usp=sharing",
            },
            {
                "id": 6,
                "mediaType": "link",
                "title": "https://drive.google.com/file/d/1rFBqrJp4QyrWVkrDV0IYe_dEUU0sYFEl/view?usp=sharing",
            }
        ],
        "user": {
            "id": 6,
            "company": "dfsgsd",
            "address": "Komitas 35A",
        },
        "order": {
            "id": 1,
            "type": "type 1"
        },
        "children": [
            {
                "id": 3,
                "label": "example",
                "parentId": 1,
                "media": [
                    {
                        "id": 8,
                        "mediaType": "image",
                        "title": "img-1cb8.png",
                    },
                    {
                        "id": 9,
                        "mediaType": "image",
                        "title": "sl3-a650.jfif",
                    },
                    {
                        "id": 7,
                        "mediaType": "image",
                        "title": "img-1026.jpg",
                    }
                ],
                "user": null,
                "order": null,
                "children": []
            },
            {
                "id": 12,
                "label": "example 3",
                "parentId": 1,
                "media": [
                    {
                        "id": 12,
                        "mediaType": "image",
                        "title": "Screenshot_1-9a68.png",
                    },
                    {
                        "id": 13,
                        "mediaType": "image",
                        "title": "Screenshot_2-5c8f.png",
                    },
                    {
                        "id": 14,
                        "mediaType": "image",
                        "title": "Screenshot_3-8492.png",
                    },
                    {
                        "id": 15,
                        "mediaType": "image",
                        "title": "Screenshot_4-92da.png",
                    },
                    {
                        "id": 16,
                        "mediaType": "pdf",
                        "title": "invoice-75380-98c4.pdf",
                    }
                ],
                "user": null,
                "order": null,
                "children": [
                    {
                        "id": 13,
                        "label": "sub example 1",
                        "parentId": 12,
                        "media": [],
                        "user": null,
                        "order": null,
                        "children": []
                    }
                ]
            },
            {
                "id": 4,
                "label": "example 2",
                "parentId": 1,
                "media": [],
                "user": null,
                "order": null,
                "children": []
            }
        ]
    },
    {
        "id": 2,
        "label": null,
        "parentId": null,
        "media": [
            {
                "id": 10,
                "mediaType": "image",
                "title": "Screenshot_1-4f31.png",
            },
            {
                "id": 11,
                "mediaType": "image",
                "title": "Screenshot_2-d8d8.png",
            }
        ],
        "user": {
            "id": 6,
            "company": "dfsgsd",
            "address": "Komitas 35A",
        },
        "order": {
            "id": 2,
            "type": "type 2"
        },
        "children": [
            {
                "id": 8,
                "label": "folder 1",
                "parentId": 2,
                "media": [],
                "user": null,
                "order": null,
                "children": []
            },
            {
                "id": 9,
                "parentId": 2,
                "media": [],
                "user": null,
                "order": null,
                "children": [
                    {
                        "id": 10,
                        "label": "srtt",
                        "parentId": 9,
                        "media": [],
                        "user": null,
                        "order": null,
                        "children": []
                    }
                ]
            }
        ]
    }
]

код которым пытался пробежаться по дереву

function getChildren(t) {
  if (t.map(item=>item.children) === undefined) {
    return;
  }
  t.map(item=>item.children).forEach((child) => {
    console.log(child.label);
   return getChildren(child);
  });
}


Ответы (0 шт):