json内容格式的转换,目的是为了使用el的树图组件



```javascript
    [
        [{
            label: 'a',
            selectt: [{
                valuee: '123'
            }],
            root: 1,
            R: 0,
            RR: -1
        }, {
            label: 'b',
            selectt: [{
                valuee: '123'
            }],
            root: 1,
            R: 1,
            RR: 0
        }, {
            label: 'c',
            selectt: [{
                valuee: '123'
            }],
            root: 1,
            R: 1,
            RR: 1
        }, {
            label: 'd',
            selectt: [{
                valuee: '123'
            }],
            root: 1,
            R: 1,
            RR: 2
        }],
        [{
            label: 'a',
            selectt: [{
                valuee: '123'
            }],
            root: 2,
            R: 0,
            RR: -1
        }, {
            label: 'b',
            selectt: [{
                valuee: '123'
            }],
            root: 2,
            R: 1,
            RR: 0
        }, {
            label: 'c',
            selectt: [{
                valuee: '123'
            }],
            root: 2,
            R: 1,
            RR: 1
        }, {
            label: 'd',
            selectt: [{
                valuee: '123'
            }],
            root: 2,
            R: 1,
            RR: 2
        }]
    ]

    root可以不用管, R = 0 属于一级节点 R > 0 且RR = -1 或0属于二级节点 R > 0 且RR > 0 属于三级节点
    我想用el的组件, 需要变换json的内容形式, 但每次到了三级节点就会出错
    前端刚接触不久, 我也只会用笨办法, 遍历 + 判断得到一级节点, 再再其中遍历 + 判断……
    避免贻笑大方, 代码我就不贴出来了, 这个问题困扰了我四天了, 尝试了五六次都不行,
    希望解决方法不要依靠label或者valuee的值
    诉求是上面的形式变成下边这样的:

```javascript
[{
        id: 1,
        label: 'a:123',
        children: [{
            id: 3,
            label: 'b123',
            children: [{
                id: 5,
                label: 'c123',
            }, {
                id: 6,
                label: 'd123',
            }]
        }]
    }, {
        id: 2,
        label: 'a:123',
        children: [{
            id: 4,
            label: 'b123',
            children: [{
                id: 7,
                label: 'c123',
            }, {
                id: 8,
                label: 'd123',
            }]
        }]
    }]

希望能给出可以使用的代码,如果应用时遇到问题,我会继续追问到可以正常使用为止。
我不会问该问题应用范围外的问题,请放心。

哈喽,代码大概是这样,一次遍历存长度,为了设置id,二次遍历设置数据:有用请点采纳哦~


      const arr = [
        [
          {
            label: "a",
            selectt: [
              {
                valuee: "123",
              },
            ],
            root: 1,
            R: 0,
            RR: -1,
          },
          {
            label: "b",
            selectt: [
              {
                valuee: "123",
              },
            ],
            root: 1,
            R: 1,
            RR: 0,
          },
          {
            label: "c",
            selectt: [
              {
                valuee: "123",
              },
            ],
            root: 1,
            R: 1,
            RR: 1,
          },
          {
            label: "d",
            selectt: [
              {
                valuee: "123",
              },
            ],
            root: 1,
            R: 1,
            RR: 2,
          },
        ],
        [
          {
            label: "a",
            selectt: [
              {
                valuee: "123",
              },
            ],
            root: 2,
            R: 0,
            RR: -1,
          },
          {
            label: "b",
            selectt: [
              {
                valuee: "123",
              },
            ],
            root: 2,
            R: 1,
            RR: 0,
          },
          {
            label: "c",
            selectt: [
              {
                valuee: "123",
              },
            ],
            root: 2,
            R: 1,
            RR: 1,
          },
          {
            label: "d",
            selectt: [
              {
                valuee: "123",
              },
            ],
            root: 2,
            R: 1,
            RR: 2,
          },
        ],
      ];
      const res = []; // 结果
      const len = [0, 0]; // 存长度,第一个为二级长度,第二个为三级长度
      arr.forEach(out => {
        out.forEach(v => {
          if (v.R > 0) {
            v.RR <= 0?len[0]++:len[1]++;
          }
        });
      });
      const index = [0, 0];  // 存下标,第一个为二级下标,第二个为三级下标
      arr.forEach((out, i) => {
        res.push({
          id: i + 1,
          children: [],
        });
        out.forEach((v) => {
          const sel = v.selectt.map((v) => v.valuee).join(","); // valuee转字符串
          const label = `${v.label}:${sel}`; // 拼接label
          if (v.R == 0) {
            // 一级
            res[i].label = label;
          } else if (v.R > 0) {
            if (v.RR <= 0) {
              // 二级
              res[i].children.push({
                id: arr.length + index[0] + 1,
                label,
                children: [],
              });
              index[0]++;
            } else {
              // 三级
              res[i].children[v.R - 1].children.push({
                id: arr.length + len[0] + index[1] + 1,
                label,
                children: [],
              });
              index[1]++;
            }
          }
        });
      });
      console.log(res)

大概写了个,结构是满足了,不知道是否还有其他的要求,特别是二级节点只有1个?多个的话从原始数据看不出2级和3级的对应关系

img

<script>
    var data =
        [
            [{
                label: 'a',
                selectt: [{
                    valuee: '123'
                }],
                root: 1,
                R: 0,
                RR: -1
            }, {
                label: 'b',
                selectt: [{
                    valuee: '123'
                }],
                root: 1,
                R: 1,
                RR: 0
            }, {
                label: 'c',
                selectt: [{
                    valuee: '123'
                }],
                root: 1,
                R: 1,
                RR: 1
            }, {
                label: 'd',
                selectt: [{
                    valuee: '123'
                }],
                root: 1,
                R: 1,
                RR: 2
            }],
            [{
                label: 'a',
                selectt: [{
                    valuee: '123'
                }],
                root: 2,
                R: 0,
                RR: -1
            }, {
                label: 'b',
                selectt: [{
                    valuee: '123'
                }],
                root: 2,
                R: 1,
                RR: 0
            }, {
                label: 'c',
                selectt: [{
                    valuee: '123'
                }],
                root: 2,
                R: 1,
                RR: 1
            }, {
                label: 'd',
                selectt: [{
                    valuee: '123'
                }],
                root: 2,
                R: 1,
                RR: 2
            }]
        ];
    var newdata = [];
    data.forEach(item => {
        var root = item.find(i => i.R == 0);
        var nitem = { label: root.label + ':' + root.selectt[0].valuee, children: [] }

        var secitem = item.find(i => i.R > 0 && (i.RR == -1 || i.RR == 0));
        var nsecitem = { label: secitem.label + ':' + secitem.selectt[0].valuee, children: [] };
        nitem.children.push(nsecitem);

        item.filter(i => i.R > 0 && i.RR > 0).forEach(thirditem => {
            nsecitem.children.push({ label: thirditem.label + ':' + thirditem.selectt[0].valuee });
        });

        newdata.push(nitem);

    });
    var id = 1;
    newdata.forEach(item => {
        item.id = id++;
    });
    newdata.forEach(item => {
        item.children.forEach(item => { item.id = id++; })
    });
    newdata.forEach(item => {
        item.children.forEach(item => {
            item.children.forEach(item => { item.id = id++ });
        })
    });

    console.log(JSON.stringify(newdata,null,2))
</script>

    let data = [
        [{
            label: 'a',
            selectt: [{
                valuee: '123'
            }],
            root: 1,
            R: 0,
            RR: -1
        }, {
            label: 'b',
            selectt: [{
                valuee: '123'
            }],
            root: 1,
            R: 1,
            RR: 0
        }, {
            label: 'c',
            selectt: [{
                valuee: '123'
            }],
            root: 1,
            R: 1,
            RR: 1
        }, {
            label: 'd',
            selectt: [{
                valuee: '123'
            }],
            root: 1,
            R: 1,
            RR: 2
        }],
        [{
            label: 'a',
            selectt: [{
                valuee: '123'
            }],
            root: 2,
            R: 0,
            RR: -1
        }, {
            label: 'b',
            selectt: [{
                valuee: '123'
            }],
            root: 2,
            R: 1,
            RR: 0
        }, {
            label: 'c',
            selectt: [{
                valuee: '123'
            }],
            root: 2,
            R: 1,
            RR: 1
        }, {
            label: 'd',
            selectt: [{
                valuee: '123'
            }],
            root: 2,
            R: 1,
            RR: 2
        }]
    ];
//扁平化
let data1 = data.flat();
data1.forEach(v=>{
   if(!result[`root_${v.root}`]){
      result[`root_${v.root}`]={l1:[],l2:[],l3:[]};
    }; 
   if(v.R===0){
     result[`root_${v.root}`].l1.push({id:v.root,label:`${v.label}:${v.selectt[0].valuee}`});
   } 
   if(v.R>0&&(v.RR<=0)){
     result[`root_${v.root}`].l2.push({id:v.root,label:`${v.label}:${v.selectt[0].valuee}`});
   }
   if(v.R>0&&(v.RR>0)){
     result[`root_${v.root}`].l3.push({id:v.root,label:`${v.label}:${v.selectt[0].valuee}`});
   } 
});

let final=[];//这个就是最后的结果
Object.keys(result).forEach(v=>{let temp = {id:result[v].l1[0].id,label:result[v].l1[0].label,children:result[v].l2}; temp.children.children=result[v].l3; final.push(temp)});

console.log(final);//打印一下结果

这个转换应该是在后台实现更好哦,使用JSONObject,JSONArray对象嵌套实现。