将数据转为树结构数据

 1.要转的数据结构,主要根据id和pid来区分节点

{
        "id": 1,
        "pid": 0,
        "label": "组件传值"
    },
    {
        "id": 2,
        "pid": 1,
        "label": "父向子传值"
    },
    {
        "id": 3,
        "pid": 2,
        "label": "prop"
    },{
        "id": 4,
        "pid": 3,
        "label": "写法"
    },
    {
        "id": 5,
        "pid": 4,
        "label": "子组件"
    },
    {
        "id": 6,
        "pid": 5,
        "label": "数组型:props:['value']"
    },
    {
        "id": 7,
        "pid": 5,
        "label": '<img src="https://tse1-mm.cn.bing.net/th/id/OIP-C.O4vKZPMT-5--cV-C7xuP0AHaEC?w=313&h=180&c=7&r=0&o=5&pid=1.7" alt="" srcset="">'
    },
    {
        "id": 8,
        "pid": 5,
        "label": "对象型:props:{value:{type:String,required:\ntrue,default:\"21\",validator(value){return ['\nsuccess','12'.'21'],}}}"
    },
    {
        "id": 9,
        "pid": 3,
        "label": "用法"
    },

转后的数据结构 


  {
    "id": 3,
    "pid": 2,
    "label": "prop",
    "children": [
      {
        "id": 4,
        "pid": 3,
        "label": "写法",
        "children": [
          {
            "id": 5,
            "pid": 4,
            "label": "子组件",
            "children": [
              {
                "id": 6,
                "pid": 5,
                "label": "数组型:props:['value']"
              },
              {
                "id": 7,
                "pid": 5,
                "label": "<img src=\"https://tse1-mm.cn.bing.net/th/id/OIP-C.O4vKZPMT-5--cV-C7xuP0AHaEC?w=313&h=180&c=7&r=0&o=5&pid=1.7\" alt=\"\" srcset=\"\">"
              },
              {
                "id": 8,
                "pid": 5,
                "label": "对象型:props:{value:{type:String,required:\ntrue,default:\"21\",validator(value){return ['\nsuccess','12'.'21'],}}}"
              }
            ]
          }
        ]
      },
      

通过循环判断当前节点是否为根节点,不是根节点的话就通过getparent函数找到它的父节点,插入到父节点的children中, getparent函数通过递归深层查找每一层节点

const getobject = (arr) => {
    let children = []

    function getparent(arr, parent) {
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].id == parent[0].pid) {
                if (arr[i].children) {
                    arr[i].children.push(parent[0])
                } else {
                    arr[i].children = parent
                }

                return arr
            }
            if (arr[i].children) {
                arr[i].children = getparent(arr[i].children, parent)

            }

        }
        return arr
    }

    for (let i = 0; i < arr.length; i++) {
        if (arr[i].pid && arr[i].pid != 0) {
            children.push(arr[i])
            arr.splice(i, 1)
            arr = getparent(arr, children)
            children = []
            i--
        }
    return arr
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值