2016-09-14 48 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; 
}