2016-03-10 73 views
0

我绑定遍历JavaScript中的分层树来确定它有多少级别。这里是我的树的短片段:分层树中递归

parent: [ 
    { id: 1 } 
    { 
     child1: [ 
      { id: 2 } 
      { 
       child2: [ 
        { id: 3 } 
        {} 
       ] 
      } 
     ], 
     child3: [ 
      { id: 4 } 
      { 
       child4: [ 
        { id: 5 } 
        {} 
       ], 
       child5: [ 
        { id: 6 } 
        { 
         child6: [ 
          { id: 7 } 
          {} 
         ] 
        } 
       ] 
      } 
     ] 
    } 
] 

会有未知数的父母和孩子。有一定的把握:

  • 每个元素(例如,父母)总是有它的数组中的2个对象。 第一个对象始终是一个ID。 第二个对象包含它拥有的子项。这可能是空的或填充

我的目标是确定树的层数。例如,此示例树中有4个级别(parent = 1,child1 + child3在同一级别(2),child4和child5在同一级别(3),child6 = 4)。

这是我到目前为止的代码:

for (var j in dependencyTree) { 
    if (getObjectSize(dependencyTree[j][1]) > 0) { 
     levelsArray.push(j + ': ' + recursiveFunction(dependencyTree[j][1], 1)); 
    } 
} 


function recursiveFunction(obj, lvls) { 
    if (getObjectSize(obj) > 0) { 
     for (var i in obj) { 
      recursiveFunction(obj[i][1], lvls++); 
     } 
    } 
    return lvls; 
} 

getObjectSize()仅返回对象的大小。即有多少直接的孩子。例如,对象parent将返回2(child1child3)。

在开始的时候,顶级parent的孩子都进入了这个函数。

我想我的问题是,for环路(for (var i in obj)),因为可能会抢第一个孩子parent有(child1),并最终将返回级的数量child1具有即使child3有更多。

任何帮助表示赞赏。

(还没有尝试lodash但都被告知它不提供递归帮助)

编辑

{ 
    "Mobile": [ 
     { 
      "id": 89 
     }, 
     { 
      "Mobile Client": [ 
       { 
        "id": 100 
       }, 
       {} 
      ] 
     } 
    ], 
    "Service Platform": [ 
     { 
      "id": 90 
     }, 
     { 
      "Service Platform": [ 
       {..." 

EDIT(新提出的格式)

我刚才讲与我的同事一样,新提出的数据格式是:

[ 
    { 
     "name": "Mobile", 
     "id": 89, 
     "children": [ 
      { 
       "name": "Mobile Client", 
       "id": 100, 
       "children": {} 
      } 
     ] 
    } 
]; 

这似乎是更可行的数据并即将实施的明天

+0

你有过的数据格式的任何控制?看起来数据是以一种奇怪的方式编码的,具体来说,有些数组应该是对象而对象应该是数组。 – Daniel

+0

@Daniel我正在与通过REST Call提供数据的人交谈,他说可以操纵它。你认为它可以更好地格式化吗? – wmash

+0

我会从使用有效的JSON开始。这意味着你拥有的任何对象都应该被命名,否则使用类似数组的方式遍历对象 – Daniel

回答

1

尽管格式,这种解决方案遍历数组中的所有元素,以及在objecs和计数。

function count(array) { 
 
    var c = 0; 
 
    array.forEach(function (a) { 
 
     c++; 
 
     if (typeof a === 'object') { 
 
      Object.keys(a).forEach(function (k) { 
 
       if (Array.isArray(a[k])) { 
 
        c += count(a[k]); 
 
       } 
 
      }); 
 
     } 
 
    }); 
 
    return c; 
 
} 
 

 
var parent = [{ id: 1 }, { child1: [{ id: 2 }, { child2: [{ id: 3 }, {}, ] }], child3: [{ id: 4 }, { child4: [{ id: 5 }, {}], child5: [{ id: 6 }, { child6: [{ id: 7 }, {}] }] }] }], 
 
    newFormat = [{ "name": "Mobile", "id": 89, "children": [{ "name": "Mobile Client", "id": 100, "children": {} }] }]; 
 

 
document.write('<pre>' + JSON.stringify(count(parent), 0, 4) + '</pre>'); 
 
document.write('<pre>' + JSON.stringify(parent, 0, 4) + '</pre><hr>'); 
 
document.write('<pre>' + JSON.stringify(count(newFormat), 0, 4) + '</pre>'); 
 
document.write('<pre>' + JSON.stringify(newFormat, 0, 4) + '</pre>');

1

这里是一些示例代码,给你一个想法如何遍历数据 - 信息是通过控制台可见。

var a = [ 
 
    { id: 1 }, 
 
    { 
 
    child1: [ 
 
     { id: 2 }, 
 
     { 
 
      child2: [ 
 
       { id: 3 }, 
 
       {} 
 
      ] 
 
     } 
 
    ], 
 
    child3: [ 
 
     { id: 4 }, 
 
     { 
 
      child4: [ 
 
       { id: 5 }, 
 
       {} 
 
      ], 
 
      child5: [ 
 
       { id: 6 }, 
 
       { 
 
        child6: [ 
 
         { id: 7 }, 
 
         {} 
 
        ] 
 
       } 
 
      ] 
 
     } 
 
    ] 
 
    } 
 
]; 
 

 
var getNumChildren=function(obj){ 
 
    var a = 0; 
 
    if(obj[1]){ 
 
for (var key in obj[1]) { 
 
    a++; 
 
    var res = getNumChildren(obj[1][key]); 
 
    console.log(res,a); 
 
    a += res; 
 
} 
 
    } 
 
    return a; 
 
} 
 

 

 
console.log(getNumChildren(a));

只要格式化数据所说,这种格式可能会更有意义和更容易理解和使用

[ 
    {"id":1, 
    "children":[ 
     {"id":2,"children":[ 
     {"id":4,"children":[]}, 
     {"id":5,"children":[]} 
     ]}, 
     {"id":3,"children":[]} 
    ] 
    } 
] 

编辑

i如果您更新数据格式,则可以使用此代码。

var data =[ 
 
    {"id":1, 
 
    "children":[ 
 
     {"id":2,"children":[ 
 
     {"id":4,"children":[]}, 
 
     {"id":5,"children":[]} 
 
     ]}, 
 
     {"id":3,"children":[]} 
 
    ] 
 
    }, 
 
    {"id":6,"children":[]} 
 
] 
 

 
var getNumChildren=function(ca){ 
 
    var n = ca.length; 
 
    ca.map(function(c){n += getNumChildren(c.children);}) 
 
    return n 
 
} 
 

 
document.write("result: " + getNumChildren(data));

+0

数据的格式将被编辑。为了清晰起见,请查看我的上述编辑感谢您的帮助! – wmash