H5W3
当前位置:H5W3 > 其他技术问题 > 正文

前端遍历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

回答

image.png

image.png

未经允许不得转载:H5W3 » 前端遍历tree结构将数组里最后一个children添加标识

赞 (0)

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址