2016-09-14 93 views
-1

我有以下結構的JSON:搜索動態JSON通過ID

{ 
"root": { 
    "containers": [ 
     { 
     "id": UNIQUE_ID, 
     ... 
     "child": [ 
      { 
      "id": UNIQUE_ID, 
      ... 
      "child": [...] 
      } 
     ] 
     }, 

     { 
     "id": UNIQUE_ID, 
     ... 
     "child": [...] 
     } 
    ] 
    } 
} 

root.containers和root.containers.child具有相同的結構。問題是我可以有無限的嵌套,並且事先不知道子節點的總數是多少,因爲它們是動態添加到這個JSON中的。

我需要一個函數,它只返回給定ID爲參數的特定對象。所以它必須潛入JSON中,直到找到具有該ID的孩子。我用.filters嘗試了一些東西,但我無法弄清楚如何深入搜索。可能一些搜索算法,我從來沒有在JavaScript中實現...

有人可以給我一個想法,我怎麼能做到這一點?謝謝!

+4

1)編寫的代碼2)執行代碼3)調試代碼。我們(可能)幫助#3。其他兩個完全是你的責任。 –

+1

如果使用遞歸,任意嵌套的對象很容易遍歷。只要循環通過孩子,並且如果孩子有任何孩子就進行遞歸調用。 – 4castle

+0

@MarcB是的,想法是我真正想問的所有問題..很抱歉,如果我寫的東西讓你覺得我在爲我做我的工作。我只是沒有更多的線索如何做到這一點。我花了好幾天的時間在這裏,所以我來到這裏試圖找人來啓發我,也許給我一些道路,我也嘗試在這裏搜索類似的問題,但沒有成功。就這樣。 –

回答

1

,你需要的功能是:

function findById(data, id){ 
    var found; 
    data.forEach(function(o){ 
     if(found){ 
      return; 
     } 
     found = o.id === id && o || o.child && findById(o.child, id); 
    }); 

    return found; 
} 

而且,它還將以這種方式使用:

findById(data.root.containers, 1) 

檢查並運行以下代碼段。它有一些測試,包括一個失敗的案例。

var data = { 
 
"root": { 
 
    "containers": [ 
 
     { 
 
     "id": 1, 
 
     "child": [ 
 
      { 
 
      "id": 2, 
 
      "child": [{ 
 
       \t id: 3 
 
      }, { 
 
       \t id: 4 
 
      }] 
 
      } 
 
     ] 
 
     }, 
 
     { 
 
     "id": 5, 
 
     "child": [{ 
 
      \t id: 6 
 
      }] 
 
     }, 
 
     { 
 
     "id": 7, 
 
     "child": [] 
 
     } 
 
    ] 
 
    } 
 
}; 
 

 
function findById(data, id){ 
 
\t var found; 
 
\t data.forEach(function(o){ 
 
\t \t if(found){ 
 
\t \t \t return; 
 
\t \t } 
 
\t \t found = o.id === id && o || o.child && findById(o.child, id); 
 
\t }); 
 

 
\t return found; 
 
} 
 

 
[1,2,3,4,5,6,7,8].forEach(function(v){ 
 
\t console.log('==== Searching for:', v); 
 
\t console.log(findById(data.root.containers, v)); 
 
});

1

您可以使用這樣的遞歸函數(https://jsfiddle.net/17qLjufc/):

//this is just a function to check for null or undefined 
var notEmpty = function(something){ 
    if(typeof(something) !== 'undefined' && something !== null){ 
     return true; 
    } 
    return false; 
} 

//this is a recursive function that does the search in dept indefinetly (supposing that all the nodes from the containers node on just have child properties) 
var findNodeById = function (node, id){ 
    if(notEmpty(node) && notEmpty(node.id) && node.id == id) 
     return node; 
    else{ 
     if(notEmpty(node) && notEmpty(node.child)){ 
      for (var i = 0 ; i < node.child.length; i++){ 
       var found = findNodeById(node.child[i], id); 
       if(found != null) 
        return found; 
       } 
      } 
     } 
    return null; 
} 

//this is going through the containers children and call the search for each of them until the first is found. 
var searchById = function(root, id){ 
    var found; 
    if(notEmpty(root) && notEmpty(root.containers)){ 
     for(var i = 0; i < root.containers.length; i++){ 
      found = findNodeById(root.containers[i], id); 
      if(found !== null){ 
       break; 
      } 
     } 
    } 
    return found; 
}