vue 遍历四维数组并存储所需的值

版权声明:1.版权归原作者Moment ° 回忆 ✨所有; 2.未经原作者允许不得转载本文内容,否则将视为侵权; 3.转载或者引用本文内容请注明来源及原作者; 4.对于不遵守此声明或者其他违法使用本文内容者,本人依法保留追究权等。 https://blog.csdn.net/qq_35366269/article/details/83344161

遍历数据:

[
    {
        "children": [
            {
                "children": [
                    {
                        "children": [
                            {
                                "createtime": "2018-10-22 17:01:00",
                                "fatherid": "0A0F402F-670F-4696-9D9C-42F0E0D665A4",
                                "id": "0A0F402F-670F-4696-9D9C-42F0E0D665A5",
                                "level": "4",
                                "name": "Four Program",
                                "type": "prog"
                            }
                        ],
                        "createtime": "2018-10-22 16:36:00",
                        "fatherid": "0A0F402F-670F-4696-9D9C-42F0E0D665A3",
                        "id": "0A0F402F-670F-4696-9D9C-42F0E0D665A4",
                        "level": "3",
                        "name": "Three Program",
                        "type": "prog"
                    }
                ],
                "createtime": "2018-10-22 14:21:00",
                "fatherid": "0A0F402F-670F-4696-9D9C-42F0E0D665A2",
                "id": "0A0F402F-670F-4696-9D9C-42F0E0D665A3",
                "level": "2",
                "name": "Test Program",
                "type": "prog"
            }
        ],
        "createtime": "2016-06-17 14:32:48",
        "haschild": 0,
        "id": "0A0F402F-670F-4696-9D9C-42F0E0D665A2",
        "level": "1",
        "name": "My Program",
        "path": "My Program",
        "type": "prog"
    },
    {
        "createtime": "2016-06-17 14:32:48",
        "haschild": 0,
        "id": "0A0F402F-670F-4696-9D9C-42F0E0D665A1",
        "level": "1",
        "name": "Shared Program",
        "path": "Shared Program",
        "type": "prog"
    },
    {
        "children": [
            {
                "createtime": "2018-10-22 17:15:00",
                "fatherid": "0A0F402F-670F-4696-9D9C-42F0E0D665A0",
                "id": "0A0F402F-670F-4696-9D9C-42F0E0D665A6",
                "level": "2",
                "name": "Test System program",
                "type": "prog"
            }
        ],
        "createtime": "2016-06-17 14:32:48",
        "haschild": 0,
        "id": "0A0F402F-670F-4696-9D9C-42F0E0D665A0",
        "level": "1",
        "name": "System Program",
        "path": "System Program",
        "type": "prog"
    }
]

遍历代码:

//目录
      initCatalog() {
        let params = {
          treeType: "prog",
        }
        let this_ = this;
        APITreeList.treeList(params)
          .then(function (response) {
            console.log(response)
            //(3) [{…}, {…}, {…}]
            /*
            {
            label: '一级 1',
            children: [{
              label: '二级 1-1',
              children: [{
                label: '三级 1-1-1'
              }]
            }]
          }
            *
            * */


            if (response !== null && response.length > 0) {
              for (let i = 0; i < response.length; i++) {
                let obj = response[i];
                let name = obj.name;
                let children = obj.children;

                let id = obj.id;

                let children2 = [];


                if (!util.isEmptyObject(children)) {
                  console.log("非空对象-:" + name);


                  children.forEach(function (obj, k) {
                    let id = obj.id;
                    let fatherid = obj.fatherid;
                    let name = obj.name;
                    let children =  obj.children;

                    let children3 = [];

                    if (!util.isEmptyObject(children)){
                      children.forEach(function (obj,k) {
                        let id = obj.id;
                        let fatherid = obj.fatherid;
                        let name = obj.name;
                        let children =  obj.children;

                        let children4 = [];

                        if (!util.isEmptyObject(children)){
                          children.forEach(function (obj,k) {
                            let id = obj.id;
                            let fatherid = obj.fatherid;
                            let name = obj.name;
                            let children =  obj.children;

                            children4.push({
                              label: name,
                              id: id,
                              fatherid:fatherid,
                              children:"",
                            })

                          })

                        }
                        children3.push({
                          label: name,
                          id: id,
                          fatherid:fatherid,
                          children:children4,
                        })

                      })
                    }
                    children2.push({
                      label: name,
                      id: id,
                      fatherid:fatherid,
                      children:children3,
                    })
                  });

                } else {

                  console.log("空对象-:" + name);

                }


                this_.treeData.push({
                  label: name,
                  id: id,
                  children: children2,
                })


              }
            }
          })
          .catch(function (error) {
            console.log(error)
          })
      },

猜你喜欢

转载自blog.csdn.net/qq_35366269/article/details/83344161
今日推荐