Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.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_Object_Search_Nested - Fatal编程技术网

按嵌套对象/数组javascript的特定键搜索

按嵌套对象/数组javascript的特定键搜索,javascript,object,search,nested,Javascript,Object,Search,Nested,我想通过javascript上嵌套数组/对象的特定键进行搜索,并需要返回所有层次结构,包括它的父级直到根父级,以及它的子级。 以下是示例json: let array = [ { "no": "1", "name": "abc", "child" : [ { "no": "1.1", "name": "

我想通过javascript上嵌套数组/对象的特定键进行搜索,并需要返回所有层次结构,包括它的父级直到根父级,以及它的子级。 以下是示例json:

        let array = [
        { 
          "no": "1",
          "name": "abc",
          "child" : [
              { 
                  "no": "1.1",
                  "name": "def",
                  "child" : [
                      {
                          "no": "1.1.1",
                          "name": "Foo"
                      },
                      {
                          "no": "1.1.2",
                          "name": "jkl"
                      }
                      ] 
              },
              { 
                  "no": "1.2",
                  "name": "Foo",
                  "child" : [
                      {
                          "no": "1.2.1",
                          "name": "Foo"
                      },
                      {
                          "no": "1.2.2",
                          "name": "aaaaaaa"
                      }
                      ] 
              }
          ]
          },
       { 
          "no": "2",
          "name": "abc2",
          "child" : [
              { 
                  "no": "2.1",
                  "name": "Foo",
                  "child" : [
                      {
                          "no": "1.1.1",
                          "name": "ghi"
                      },
                      {
                          "no": "1.1.2",
                          "name": "jkl"
                      }
                      ] 
              },
              { 
                  "no": "2.2",
                  "name": "ghssssi",
                  "child" : [
                      {
                          "no": "2.2.1",
                          "name": "ghssssi"
                      },    
                      {
                          "no": "2.2.2",
                          "name": "asass"
                      }
                      ] 
              }
          ]
      }
    ];
当我们想按key='Foo'搜索时,结果如下:

        array_result = [
      { 
          "no": "1",
          "name": "abc",
          "child" : [
              { 
                  "no": "1.1",
                  "name": "def",
                  "child" : [
                      {
                          "no": "1.1.1",
                          "name": "Foo"
                      }
                      ] 
              },
              { 
                  "no": "1.2",
                  "name": "Foo",
                  "child" : [
                      {
                          "no": "1.2.1",
                          "name": "Foo"
                      }
                      ] 
              }
          ]
      },
       { 
          "no": "2",
          "name": "abc2",
          "child" : [
              { 
                  "no": "2.1",
                  "name": "Foo",
                  "child" : [
                      {
                          "no": "1.1.1",
                          "name": "ghi"
                      },
                      {
                          "no": "1.1.2",
                          "name": "jkl"
                      }
                      ] 
              }
          ]
      }
    ];
我确信它将需要递归函数。有人知道吗?
谢谢

如果数组具有所需值或子数组具有该值,则可以从原始数组中获取副本并过滤该数组

var数组=[{no:“1”,name:“abc”,children:[{no:“1.1”,name:“def”,children:[{no:“1.1.1”,name:“Foo”},{no:“1.2”,name:“Foo”,children:[{no:“1.2.1”,name:“Foo”},{no:“1.2.2”,name:“aaaaaaaaaaaaaaaaaaa”}}}},{no:“2”,name:“abc2”,children no:{no:“2”,children no:“2”,children no:“Foo:”Foo:“Foo:[[1.1.1.”,姓名:“ghi”},{no:“1.1.2”,姓名:“jkl”},{no:“2.2”,姓名:“ghssssi”,儿童:[{no:“2.2.1”,姓名:“GHSSSSSI”},{no:“2.2.2”,姓名:“ASASASS”}]},
find='Foo',
result=JSON.parse(JSON.stringify(array)).filter(函数搜索(a){
儿童;
if(a.name==find){
返回true;
}
if(!Array.isArray(a.children)){
返回false;
}
children=a.children.filter(搜索);
if(儿童长度){
a、 儿童=儿童;
返回true;
}
});
控制台日志(结果)

作为控制台包装{max height:100%!important;top:0;}
到目前为止,您自己做了哪些尝试?向我们展示您的代码,以便有人指出问题所在。这里不是获取完整代码答案的地方。您想在不改变原始数据的情况下获取原始数据的副本吗?谢谢@nina,这对我帮助很大。我已经根据需要修改了代码。