js遍历树结构

qq_34222567 2017-11-08 07:39:46
我想遍历这样的数据,得到qx为1的节点,和这个节点的直接子节点,数据格式为这样
[
{
"thisNode": "10000480",
"prientNode": "",
"qx": "0",
"children": [
{
"prientNode": "10000480",
"thisNode": "10000470",
"qx": "0",
"children": []
},
{
"prientNode": "10000480",
"thisNode": "10000420",
"children": [
{
"prientNode": "10000420",
"thisNode": "10000551",
"qx": "1",
"children": [
{
"prientNode": "10000420",
"thisNode": "10000551",
"qx": "1",
"children": []
}
]
}
]
}
]
}
]



...全文
534 3 点赞 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
weixin_41806047 2018-12-28
qx是什么呀
  • 打赏
  • 举报
回复
IMyxuan 2017-11-14
用递归是最简单的解决办法啦
  • 打赏
  • 举报
回复

    d = [{
        "thisNode": "10000480",
        "prientNode": "",
        "qx": "0",
        "children": [
          {
              "prientNode": "10000480",
              "thisNode": "10000470",
              "qx": "0",
              "children": []
          },
          {
              "prientNode": "10000480",
              "thisNode": "10000420",
              "children": [
                {
                    "prientNode": "10000420",
                    "thisNode": "10000551",
                    "qx": "1",
                    "children": [
                      {
                          "prientNode": "10000420",
                          "thisNode": "10000551",
                          "qx": "1",
                          "children": []
                      }
                    ]
                }
              ]
          }
        ]
    }
    ];
    function findChildren(arr, key, value,callback) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i][key] == value) callback(arr[i].children)
            findChildren(arr[i].children, key, value, callback);
        }
    }
    findChildren(d, 'qx', '1', function (children) {
        console.log(children)
    });


Web开发学习资料推荐
jqGrid colModel配置参数
Web前端开发教程
  • 打赏
  • 举报
回复
相关推荐
发帖
JavaScript
加入

8.5w+

社区成员

Web 开发 JavaScript
申请成为版主
帖子事件
创建了帖子
2017-11-08 07:39
社区公告
暂无公告