代码之家  ›  专栏  ›  技术社区  ›  activebiz

用完整结构intect在JSON树中搜索

  •  0
  • activebiz  · 技术社区  · 8 年前

    我想这是以前有人问过的 filter nested tree object without losing structure

    但我要找的恰恰相反。

    对于JSON数据

        var items = [
        {
            name: "a1",
            id: 1,
            children: [{
                name: "a2",
                id: 2,
                children: [{
                    name: "a3",
                    id: 3
                }]
            },
            {
                name: "b2",
                id: 5,
                children: [{
                    name: "a4",
                    id: 4
                }]
            }]
        }
    ];
    

    a2 . 它应该返回以下内容

    var items = [
    {
        name: "a1",
        id: 1,
        children: [{
            name: "a2",
            id: 2,
            children: [{
                name: "a3",
                id: 3
            }]
        }]
    }
    

    i、 e.树路径中的所有节点(从根到叶节点)。

    知道如何在nodejs/javascript中实现吗?

    谢谢

    1 回复  |  直到 8 年前
        1
  •  0
  •   protoproto    8 年前

    按照我的示例,您可以更改代码以适合您的工作:

    var items = [
        {
            name: "a1",
            id: 1,
            children: [{
                name: "a2",
                id: 2,
                children: [{
                    name: "a3",
                    id: 3
                }]
            },
            {
                name: "b2",
                id: 5,
                children: [{
                    name: "a4",
                    id: 4
                }]
            }]
        }
    ];
    //console.log(items);
    //first, add depth (of each element) to array items
    var depths = items;
    //structure of path = [[0,length_1st],[1,length_2nd],[2,length_3rd],[3,length_4th],...,[last,length_last]]
    var path = [];
    //for first value of path
    path.push([0,depths.length]);
    //test to add depth for depths:
    depths.map(function add_depth(current){
      current['depth'] = path[path.length-1][0];
      if(current.children){
        //continue to array children
        path.push([path[path.length-1][0]+1,current.children.length]);
        current.children.map(add_depth);
      }else{
        //get back of path
        while(path.length>1 && path[path.length-1][1]<2){
            path.pop();
        }
        //decrease length path[...[x,length]]
        path[path.length-1][1]--;
      };
    });
    //console.log(depths);
    
    // has depth in array depths, now is function for search in array depths
    function search_name(str){
        let path_result = [];
        let flagExit = false;
        depths.findIndex(function find_name(current,index){
            if (flagExit){
                return;
            };
            if(current.name===str){
                //finish at here
                path_result[current.depth] = index;
                flagExit = true;
                return;
            }else{
                if(current.children){
                    path_result[current.depth] = index;
                    current.children.findIndex(find_name);
                };
            };
        });
        return path_result;
    };
    var name_to_search = "a3";
    var path_end = search_name(name_to_search); //console.log(path_end);
    //show result from path_end:
    var result = [];
    var self_items, self_result;
    if (path_end){
        for(let i=0;i<path_end.length;i++){
            if(i===0){
                result[i] = {};
                result[i]['name'] = items[path_end[i]].name;
                result[i]['id'] = items[path_end[i]].id;
                if(i === path_end.length-1){
                    //just the first result
                    result[i]['children'] = items[path_end[i]].children;
                }else{
                    result[i]['children'] = [];
                    self_items = items[path_end[i]].children;
                    self_result = result[i]['children'];
                };
    
            }else{
                if(i !== path_end.length-1){
                    //not to the end
                    self_result[0] = {};
                    self_result[0]['name'] = self_items[path_end[i]].name;
                    self_result[0]['id'] = self_items[path_end[i]].id;
                    self_result[0]['children'] = [];
                    self_items = self_items[path_end[i]].children;
                    self_result = self_result[0]['children'];
                }else{
                    //to the end, check for the children end
                    self_result[0] = {};
                    self_result[0]['name'] = self_items[path_end[i]].name;
                    self_result[0]['id'] = self_items[path_end[i]].id;
                    if(self_items[path_end[i]].children){
                        self_result[0]['chidren'] = self_items[path_end[i]].children;
                    };
                    //check again the searching, if not match the name_to_search, set result to empty!
                    if(self_result[0]['name'] !== name_to_search){
                        result = [];
                    }
                };
    
            }
        };
        
    }else{
        result = [];
    }
    
    console.log(result);
    推荐文章