账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    【比较难】JS多层递归遍历
    58
    0

    小弟初学js,遇到一个难题,关于把一个多层数据结构的转换成treeview可识别的格式问题

    原数据格式为:

    {
      "root": "body",
      "structure": {
        "body": [
            "head",
            "foot"
        ],
        "head": [
          "control",
          "tabs"
        ],
        "control": [
          "c123",
          "c456",
          "c789"
        ],
        "tabs": [
          "33",
          "44"
        ],
        "foot": [
          "footLeft",
          "divEnd"
        ],
        "footLeft": [
          "footLeftContainer"
        ],
        "footLeftContainer": [
          "add22",
          "ffff",
          "fff"
        ],
        "divEnd": [
          "divEnd$111"
        ]
      }
    };

    期望处理成的格式为:

    
        [
      {
        text: "body",
        nodes: [
          {
            text: "head",
            nodes: [
              {
                text: "control",
                nodes: [
                  {
                    text: "c123"
                  },
                  {
                    text: "c456"
                  },
                  {
                    text: "c789"
                  }
                ]
              },
              {
                text: "tabs",
                nodes: [
                  {
                    text: "t33"
                  },
                  {
                    text: "t44"
                  }
                ]
              }
            ]
          },
          {
            text: "foot",
            nodes: [
              {
                text: "footLeft",
                nodes: [
                  {
                    text: "footLeftContainer",
                    nodes: [
                      {
                        text: "add22"
                      },
                      {
                        text: "ffff"
                      },
                      {
                        text: "fff"
                      }
                    ]
                  }
                ]
              },
              {
                text: "divEnd",
                nodes: [
                  {
                    text: "divEnd$111"
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
    

    原理就是从structure中层层找child,起始点是root节点的值(即"body")
    每个节点的child如果不在structure一级节点中,则认为是叶子节点
    想了一天,没有好办法,来求助各位前辈

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部