2015-11-03 78 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背,我們都會做:) –