怎样用js递归无限向下获取子节点数据,从而得到一个新的树形数据?

发布于 2022-09-07 20:58:29 字数 1521 浏览 17 评论 0

现有数据:

var res = [{
    code: 1,
    name: "湖北省",
    children: [{
        code: 1,
        name: "武汉市",
        children: [{
            code: 1,
            name: "汉阳区",
            children: [{
                code: 1,
                name: "水上分局"
            }]
        }, {
            code: 1,
            name: "武昌区",
            children: [{
                code: 1,
                name: "水上分局"
            }]
        }, {
            code: 1,
            name: "汉口区",
            children: [{
                code: 1,
                name: "水上分局"
            }]
        }]
    }, {
        code: 1,
        name: "十堰市",
        children: [{
            code: 1,
            name: "郧阳区",
            children: [{
                code: 1,
                name: "安阳镇"
            }]
        }, {
            code: 1,
            name: "茅箭区",
            children: [{
                code: 1,
                name: "小川乡"
            }]
        }]
    }]
}]

想递归得到下面格式的数据(就是过滤掉了部分不需要的字段):

var newData = [{
    name: '湖北省',
    children: [{
        name: '武汉市',
        children: [{
            name: '汉阳区',
            children: [{
                name: '水上分局'
            }]
            ...
        }]
    },{
        name: '十堰市',
        children: [{
            name: '郧阳区',
            children: [{
                name: '安阳镇'
            }]
            ...
        }]
    }]
}]

现在我只会用嵌套循环的方式来解决,如果想精简代码或者不知道具体有多少层的话,嵌套循环肯定是不行的,请问用递归的方法该怎么实现呢?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙气飘飘 2022-09-14 20:58:29

<script type="text/javascript">

var json=[{
    "id": "100",
    "name": "浙江省",
    "children":[{
        "id": "1100",
        "name": "杭州市",
        "children":[{
            "id": "11001",
            "name": "西湖区",
            
        },{
            "id": "11002",
            "name": "上城区",
            
        },{
            "id": "11003",
            "name": "下城区",
            
        }]
    }]
  }
  ,{
    "id": "200",
    "name": "北京",
    "children":[{
        "id": "2100",
        "name": "北京市",
        "children":[{
            "id": "21001",
            "name": "东城区",
            "children":[{
                "id": "23001",
                "name": "小罗罗",
            },{
                "id": "23002",
                "name": "大罗罗",
            }]
        },{
            "id": "21002",
            "name": "西城区",
            "children":[{
                "id": "22001",
                "name": "天安门",
            },{
                "id": "22002",
                "name": "红旗",
            }]
        }]
    }]
  }
  ];

//获得数组中每一个对象的最后一个子对象,前提是如果children中没有值,不可以放一个空的"children":[]

 var arr1 = []
 function serverArray(arr){
  for(var item = 0;item < arr.length;item++){

    if(arr[item].children){
      serverArray(arr[item].children)
    }else{
        arr1.push(arr[item])
    }
  }
  return arr1
}
console.log(serverArray(json))

</script>
得到的数据:

  1. (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
  2. 0: {id: "11001", name: "西湖区"}
  3. 1: {id: "11002", name: "上城区"}
  4. 2: {id: "11003", name: "下城区"}
  5. 3: {id: "23001", name: "小罗罗"}
  6. 4: {id: "23002", name: "大罗罗"}
  7. 5: {id: "22001", name: "天安门"}
  8. 6: {id: "22002", name: "红旗"}
  9. length: 7
  10. __proto__: Array(0)
烦人精 2022-09-14 20:58:29
function serverArray(arr){
  for(var item = 0;item < arr.length;item++){
    if(arr[item].children){
      delete arr[item].code
      serverArray(arr[item].children)
    }
  }
  return arr
}
console.log(serverArray(res))

是这个意思?

鲜肉鲜肉永远不皱 2022-09-14 20:58:29

必须反驳 “循环是不行的”

function s(){
    let n = res;
    let r = [...n];
    do{
        let N_r = [];
        for(let node of r){
            delete(node["code"]);
            if(node.children){
                N_r = N_r.concat(node.children);
            }
        }
        r = N_r;
    }while(r.length);
    return n; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文