js一个树形结构,怎么遍历它增加需要的属性

发布于 2022-09-13 01:11:38 字数 1535 浏览 22 评论 0

[{
    "children": [{
        "children": [{
            "children": [{
                "hasChildren": false,
                "name": "小苔便利店",
                "pid": 587,
                "id": 1428553074498211840,
                "label": "小苔便利店",
                "deptType": 2,
                "leaf": true
            }],
            "hasChildren": true,
            "name": "天桥区",
            "pid": 586,
            "id": 587,
            "label": "天桥区",
            "deptType": 1,
            "leaf": false
        }, {
            "hasChildren": false,
            "name": "市中区",
            "pid": 586,
            "id": 588,
            "label": "市中区",
            "deptType": 1,
            "leaf": true
        }],
        "hasChildren": true,
        "name": "济南市",
        "pid": 585,
        "id": 586,
        "label": "济南市",
        "deptType": 1,
        "leaf": false
    }, {
        "children": [{
            "hasChildren": false,
            "name": "111",
            "pid": 589,
            "id": 596,
            "label": "111",
            "deptType": 1,
            "leaf": true
        }],
        "hasChildren": true,
        "name": "青岛公司",
        "pid": 585,
        "id": 589,
        "label": "青岛公司",
        "deptType": 1,
        "leaf": false
    }],
    "hasChildren": true,
    "name": "中石油省公司",
    "pid": null,
    "id": 585,
    "label": "中石油省公司",
    "deptType": 1,
    "leaf": false
}]

这种结构的数据,如果hasChildren: true就给这一级增加isDisabled: true

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

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

发布评论

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

评论(2

塔塔猫 2022-09-20 01:11:38
function disabledTree(arr) {
  arr.forEach((el) => {
    el.isDisabled = el.hasChildren;
    disabledTree(el.children || []);
  });
}

let aa = [{
    "children": [{
        "children": [{
            "children": [{
                "hasChildren": false,
                "name": "小苔便利店",
                "pid": 587,
                "id": 1428553074498211840,
                "label": "小苔便利店",
                "deptType": 2,
                "leaf": true
            }],
            "hasChildren": true,
            "name": "天桥区",
            "pid": 586,
            "id": 587,
            "label": "天桥区",
            "deptType": 1,
            "leaf": false
        }, {
            "hasChildren": false,
            "name": "市中区",
            "pid": 586,
            "id": 588,
            "label": "市中区",
            "deptType": 1,
            "leaf": true
        }],
        "hasChildren": true,
        "name": "济南市",
        "pid": 585,
        "id": 586,
        "label": "济南市",
        "deptType": 1,
        "leaf": false
    }, {
        "children": [{
            "hasChildren": false,
            "name": "111",
            "pid": 589,
            "id": 596,
            "label": "111",
            "deptType": 1,
            "leaf": true
        }],
        "hasChildren": true,
        "name": "青岛公司",
        "pid": 585,
        "id": 589,
        "label": "青岛公司",
        "deptType": 1,
        "leaf": false
    }],
    "hasChildren": true,
    "name": "中石油省公司",
    "pid": null,
    "id": 585,
    "label": "中石油省公司",
    "deptType": 1,
    "leaf": false
}]

disabledTree(aa)
console.log(aa);

image.png

白首有我共你 2022-09-20 01:11:38

数据

let data = [{
    "children": [{
        "children": [{
            "children": [{
                "hasChildren": false,
                "name": "小苔便利店",
                "pid": 587,
                "id": 1428553074498211840,
                "label": "小苔便利店",
                "deptType": 2,
                "leaf": true
            }],
            "hasChildren": true,
            "name": "天桥区",
            "pid": 586,
            "id": 587,
            "label": "天桥区",
            "deptType": 1,
            "leaf": false
        }, {
            "hasChildren": false,
            "name": "市中区",
            "pid": 586,
            "id": 588,
            "label": "市中区",
            "deptType": 1,
            "leaf": true
        }],
        "hasChildren": true,
        "name": "济南市",
        "pid": 585,
        "id": 586,
        "label": "济南市",
        "deptType": 1,
        "leaf": false
    }, {
        "children": [{
            "hasChildren": false,
            "name": "111",
            "pid": 589,
            "id": 596,
            "label": "111",
            "deptType": 1,
            "leaf": true
        }],
        "hasChildren": true,
        "name": "青岛公司",
        "pid": 585,
        "id": 589,
        "label": "青岛公司",
        "deptType": 1,
        "leaf": false
    }],
    "hasChildren": true,
    "name": "中石油省公司",
    "pid": null,
    "id": 585,
    "label": "中石油省公司",
    "deptType": 1,
    "leaf": false
}];
const setTreeNode=(data=[],setFun=function(node){return node;})=>data.forEach(item=>(item = setFun(item),setTreeNode(item.children||[],setFun)));

setTreeNode(data, function(node){
  if(node.hasChildren) {
     node.isDisabled = true;
  }
  return node;
 });
console.log(data);

image.png

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文