Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:搜索分层树_Javascript - Fatal编程技术网

Javascript:搜索分层树

Javascript:搜索分层树,javascript,Javascript,我必须从JavaScript的层次结构树中按名称获取子对象 我的样本数据如下 { id: 2, name: "Alphabet", parent: null, path: "Alphabet", children: [ { id: 3, name: "unit3", parent: 2, path: "Alphabet/unit3",

我必须从JavaScript的层次结构树中按名称获取子对象

我的样本数据如下

{
 id: 2, 
 name: "Alphabet",
 parent: null,
 path: "Alphabet", 
 children: [ 
             { id: 3,
               name: "unit3",
               parent: 2,
               path: "Alphabet/unit3",
               children:[ 
                         { id: 5,
                           name: "unit15",
                           parent: 3,
                           path: "Alphabet/unit3/unit15",
                           children:[]
                         }
                       ] 
            },
            {  id: 4,
               name: "unit6",
               parent: 2,
               path: "Alphabet/unit6",
               children: []
            }
          ]
}
我尝试了以下方法:

getChildFromTree(treeObj,name) : any {

    if(treeObj.name == name) {
      return treeObj;
    }    
    var child;
    for(var i=0;i<treeObj.children.length;i++) {
      if(treeObj.children[i].name == name) {
        child =  treeObj.children[i];                   
        break;
      } else {
        child = this.getChildFromTree(treeObj.children[i],name);
      }
    }
    if(child) {
      return child;
    }   
  }
getChildFromTree(treeObj,name):任意{
if(treeObj.name==name){
返回treeObj;
}    
var-child;

for(var i=0;i您正在使用for(var i=0
)迭代
treeObj.children
,但是,即使您在使用递归函数this.getChildFromTree时找到一个子项,它也不会返回,因为for(var i=0没有停止(for循环内的else分支中没有中断或返回)


如果(child)返回child;在循环中,您只需添加一个
if(child)return child;

您正在使用
for(var i=0
)对
treeObj.children
进行迭代,但是即使在使用递归函数
this.getChildFromTree
时找到一个子对象,它也不会返回,因为
for(var i=0
未停止(for循环内的else分支中没有中断或返回)


你可以简单地在循环中添加一个
if(child)return child;

不太喜欢重新翻转方向盘,我建议你使用一个库。我们使用它来处理数据。只要你仔细思考,它就会非常强大。下面是你如何回答你的问题的:

//const objectScan=require('object-scan');
const find=(haystack,name)=>objectScan(['**.name']{
rtn:'父',
中止:对,
filterFn:({value})=>value==name
})(干草堆);
const data={id:2,name:'Alphabet',parent:null,path:'Alphabet',children:[{id:3,name:'unit3',parent:2,path:'Alphabet/unit3',children:[]},{id:4,name:'unit6',parent:2,path:'Alphabet/unit6',children:[]};
log(查找(数据'unit15');
/* =>
{id:5,
名称:'unit15',
家长:3,,
路径:“字母表/unit3/unit15”,
儿童:[]
*/
。作为控制台包装{最大高度:100%!重要;顶部:0}

我不太喜欢重装轮子,我建议你使用一个库。我们使用它来处理数据。只要你仔细思考它,它就会非常强大。下面是如何回答你的问题:

//const objectScan=require('object-scan');
const find=(haystack,name)=>objectScan(['**.name']{
rtn:'父',
中止:对,
filterFn:({value})=>value==name
})(干草堆);
const data={id:2,name:'Alphabet',parent:null,path:'Alphabet',children:[{id:3,name:'unit3',parent:2,path:'Alphabet/unit3',children:[]},{id:4,name:'unit6',parent:2,path:'Alphabet/unit6',children:[]};
log(查找(数据'unit15');
/* =>
{id:5,
名称:'unit15',
家长:3,,
路径:“字母表/unit3/unit15”,
儿童:[]
*/
。作为控制台包装{最大高度:100%!重要;顶部:0}

是的,我添加了内循环和外循环,现在工作正常。非常感谢#leonardfactory是的,我添加了内循环和外循环,现在工作正常。非常感谢#leonardfactory