2015-11-03 87 views
0

我有一个复杂的JavaScript对象如下。搜索JavaScript对象中所有值的路径

一个例子对象:

var object= { 
    "name": "tfifkhul", 
    "id": "262761", 
    "children": [ 
     { 
      "name": "rthrth", 
      "id": 0, 
      "children": [ 
       { 
        "name": "test", 
        "id": "262762", 
        "children": [] 
       } 
      ] 
     }, 
     { 
      "name": "rthsrth", 
      "id": 0, 
      "children": [ 
       { 
        "name": "test", 
        "id": "262762", 
        "children": [] 
       } 
      ] 
     }, 
     { 
      "name": "rthrthhrth", 
      "id": 0, 
      "children": [ 
       { 
        "name": "test", 
        "id": "262762", 
        "children": [ 
         { 
          "name": "rtjrtj", 
          "id": 0, 
          "children": [ 
           { 
            "name": "fwefwefwef", 
            "id": "262768", 
            "children": [] 
           } 
          ] 
         }, 
         { 
          "name": "hsrtjrtdjrtj", 
          "id": 0, 
          "children": [ 
           { 
            "name": "we4yhesrhy", 
            "id": "262764", 
            "children": [] 
           } 
          ] 
         }, 
         { 
          "name": "lol", 
          "id": "262763", 
          "children": [ 
           { 
            "name": "fwefwefwef", 
            "id": "262768", 
            "children": [ 
             { 
              "name": "87ok78", 
              "id": "262765", 
              "children": [ 
               { 
                "name": "78o78", 
                "id": 0, 
                "children": [ 
                 { 
                  "name": "we4yhesrhy", 
                  "id": "262764", 
                  "children": [ 
                   { 
                    "name": "test1", 
                    "id": 0, 
                    "children": [ 
                     { 
                      "name": "", 
                      "id": "262766", 
                      "children": [] 
                     } 
                    ] 
                   }, 
                   { 
                    "name": "test2", 
                    "id": 0, 
                    "children": [ 
                     { 
                      "name": "", 
                      "id": "262766", 
                      "children": [] 
                     } 
                    ] 
                   } 
                  ] 
                 } 
                ] 
               }, 
               { 
                "name": "7o78o76o8", 
                "id": 0, 
                "children": [ 
                 { 
                  "name": "", 
                  "id": "262766", 
                  "children": [] 
                 } 
                ] 
               }, 
               { 
                "name": "ko", 
                "id": 0, 
                "children": [ 
                 { 
                  "name": "", 
                  "id": "262767", 
                  "children": [] 
                 } 
                ] 
               } 
              ] 
             } 
            ] 
           } 
          ] 
         } 
        ] 
       } 
      ] 
     } 
    ] 
}; 

我需要创建一个功能来搜索与给定值时关键的“ID”的所有匹配值。

到目前为止,我已创建了一个递归函数:

function searchOccurances(theObject, value,path) { 
     var result = null; 
     if(theObject instanceof Array) { 
      for(var i = 0; i < theObject.length; i++) { 

       result = searchOccurances(theObject[i],value,path+","+i); 
      } 
     } 
     else 
     { 
      for(prop in theObject) { 

       if(prop == 'id') { 
        if(theObject[prop] == value) { 
         keyOccurances.push(path); 
        } 
       } 
       if((theObject[prop] instanceof Array) || (theObject[prop] instanceof Object)) 
       { 
        if((theObject[prop].length!=undefined)&&(theObject[prop].length!=0)) 
        { 
         result = searchOccurances(theObject[prop],value,path+","+prop); 
        } 
       } 
      } 
     } 
     return result; 
    } 
keyOccurances=[]; 
searchOccurances(object,262762,''); 
console.log(keyOccurances); 
//Output 
[",children,0,children,0", ",children,1,children,0", ",children,2,children,0"] -- correct 

keyOccurances=[]; 
searchOccurances(object,262768,''); 
console.log(keyOccurances); 

//Output 
[",children,1,children,0,children,1,children,0", ",children,1,children,0,children,2,children,0"] --wrong 

该函数返回匹配值的逗号分隔的路径阵列,但并不似乎正在正确的结果。对于值为'262762'的第一次调用给出了更正路径列表,但是值'262768'给出了不正确的路径列表。

请帮忙。

+0

你还需要答案吗? – fedeghe

回答

1

我建议提供一个更好的测试对象。你真的有这么多的'ID = 0'的孩子在真正的用例吗?你会有2个同一个ID的孩子吗?这使得调试非常困难。

下面是一个应该按预期工作的示例函数。

function search(object, value) { 
    var res = [], searchPath; 

    (searchPath = function(children, path) { 
    var n, newPath; 

    for(n in children) { 
     if(typeof children[n].id !== 'undefined' && parseInt(children[n].id, 10) === value) { 
     res.push(path); 
     } 
     newPath = path.slice(); 
     newPath.push(children[n].id); 
     searchPath(children[n].children, newPath); 
    } 
    })([ object ], []); 

    return res; 
} 

console.log(search(object, 262762)); 
console.log(search(object, 262768)); 

输出:

[["262761", 0], ["262761", 0], ["262761", 0]] 
[["262761", 0, "262762", 0], ["262761", 0, "262762", "262763"]] 

上面的代码是(还)没有防弹但希望它是足够短,以容易理解的。

+1

由于'parseInt'的问题,我推荐使用一元'+'操作符或将基数提供给'parseInt':'parseInt(children [n] .id,10);' –

+0

哦,你说得对。我不时忘记这个。固定。 – Arnauld

+0

不要汗流We背,我们都会做:) –