前端遍历tree结构将数组里最后一个children添加标识

原始数据:

      dataList: [
        {
          id: 1,
          label: '标题1',
          children: [
            {
              id: 2,
              label: '一级 1-1',
              children: [
                {
                  id: 3,
                  label: '二级 1-1-1',
                },
                {
                  id: 4,
                  label: '三级',
                  type: '1',
                  children: [
                    {
                      id: 5,
                      label: '四级 1-1-1',
                    },
                    {
                      id: 6,
                      label: '四级 1-1-2',
                    },
                    {
                      id: 7,
                      label: '四级 1-1-3',
                    },
                  ],
                },
              ],
            },
          ],
        },
      ]

期望数据:

dataList: [{
          id: 1,
          label: '标题1',
          type: '1',
          children: [
            {
              id: 2,
              label: '一级 1-1',
              type: '1',
              children: [
                {
                  id: 3,
                  label: '二级 1-1-1',
                  type: '1',
                },
                {
                  id: 4,
                  label: '三级',
                  type: '1',
                  children: [
                    {
                      id: 5,
                      label: '四级 1-1-1',
                      type: '1',
                    },
                    {
                      id: 6,
                      label: '四级 1-1-2',
                    },{
                      id: 7,
                      label: '四级 1-1-3',
**                      type: '2',**
                    },
                  ],
                },
              ],
            },
          ],
        },
      ]

也就是说最后一个type为2 最后个之前的children添加的type为1

阅读 2.7k
1 个回答

image.png

image.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题