How to insert json dom to current json type?

How to insert json dom to current json type?
in my project, I have a question, how can insert json dom to current json type?

this is my code.

foreach() 
if (r.part == 4) {
            treeData = {
              name: r.part1,
              isDirectory: true,
              items: [
                {
                  name: r.part2,
                  isDirectory: true,
                  items: [
                    {
                      name: r.part3,
                      isDirectory: true,
                      items: [
                        {
                          name: r.part4,
                          isDirectory: true,
                          items: [
                            {
                              name: r.name,
                              isDirectory: false,
                            },
                          ],
                        },
                      ],
                    },
                  ],
                },
              ],
            };
          }

when part = 4 , I will show this json type, but only first date will show all, after second date need to insert under code

{
                          name: r.part4,
                          isDirectory: true,
                          items: [
                            {
                              name: r.name,
                              isDirectory: false,
                            },
                          ],
                        },

so I hope the json type like this one

{
    part: "4",
    name: "Offiecr",
    isDirectory: true,
    items: [
      {
        name: "Report",
        isDirectory: true,
        items: [
          {
            name: "SelfReport",
            isDirectory: true,
            items: [
              {
                name: "Test",
                isDirectory: true,
                items: [
                  {
                    name: "1100101.pdf",
                    isDirectory: false
                  },
                ]
              }
            ]
          },
            {
            name: "ChangeManage",
            isDirectory: true,
            items: [
              {
                name: "Officer.docx",
                isDirectory: false
              }
            ]
          }
        ]
      }
    ]
  },

but I will get this one

{
    "name": "Officer",
    "isDirectory": true,
    "items": [
        {
            "name": "Report",
            "isDirectory": true,
            "items": [
                {
                    "name": "SelfReport",
                    "isDirectory": true,
                    items: [
                    {
                     name: "Test",
                    isDirectory: true,
                    items: [
                      {
                        name: "1100101.pdf",
                        isDirectory: false
                      },
                    ]
              }
                }
            ]
        }
    ]
},
{
    "name": "Officer",
    "isDirectory": true,
    "items": [
        {
            "name": "Report",
            "isDirectory": true,
            "items": [
                {
                    "name": "SelfReport",
                    "isDirectory": true,
                     items: [
                    {
                     name: "Test",
                    isDirectory: true,
                    items: [
                      {
                        name: "1100101.pdf",
                        isDirectory: false
                      },
                    ]
                }
            ]
        }
    ]
},

so how could do I solve this problem?