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": []
}
]
}
]
}
]
}
]



...全文
816 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
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前端开发教程

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧