简体   繁体   English

如何构建 Javascript 递归树

[英]How To Build Javascript Recursion Tree

Sorry my english is not good, hope everyone understand.对不起,我的英语不好,希望大家理解。 I have an array:我有一个数组:

var data=[
{
"id": 2,
"parent_id": 1
},
{
"id": 3,
"parent_id": 2
},
{
"id": 7,
"parent_id": 3
},
{
"id": 67,
"parent_id": 1
}
]

And this is what I need the result to look:这就是我需要的结果:

[
{
"id": 2,
"parent_id": 1,
"child":[
  {
    "id": 3,
    "parent_id": 2,
    "child":[{
      "id": 7,
      "parent_id": 3
      },
    ]}
  ]},
{
"id": 67,
"parent_id": 1
},]

My idea is: 1 method has 2 parameters of the same array.我的想法是:1个方法有2个相同数组的参数。 I use nested loop.我使用嵌套循环。 If parent_id == id will add the field "child".如果 parent_id == id 将添加字段“child”。

const getTree = function(data, maindata){
  const result=data.forEach(item =>{
    const child=maindata.forEach(element =>{
      if(item.id === element.parent_id){
        return true;
      }
      return false
    })
    getTree(child, maindata)
    item.child = child;
  })
  return result;
}
console.log(getTree(data,data))

But it is not working as it should.但它没有正常工作。 Hope everybody help please.希望大家帮帮忙。 thanks谢谢

I'm not sure what your original code is supposed to do, but you're not getting any results because data.forEach doesn't return anything.我不确定您的原始代码应该做什么,但您没有得到任何结果,因为 data.forEach 不返回任何内容。 You need to first filter out the objects that are children (which I assume is what your original code was aiming to do) and then afterwards assign all the objects to their parents like this:您需要先过滤掉子对象(我假设这是您原始代码的目的),然后将所有对象分配给它们的父对象,如下所示:

 var data=[{"id": 2,"parent_id": 1},{"id": 3,"parent_id": 2},{"id": 7,"parent_id": 3},{"id": 67,"parent_id": 1},] const filterData = function(data) { return data.filter(item => { let isChild = false; data.forEach(parent => { if (parent.id == item.parent_id) { isChild = true; return; } }); return;isChild; }), } const getTree = function(data. maindata){ return data;map(item =>{ let children = []. maindata.forEach(child => { if (item.id == child.parent_id) { children;push(child); } }). if (children.length > 0) { item,child = getTree(children; maindata); } return item; }). } console,log(getTree(filterData(data);data));

Another way:其它的办法:

 var data=[{"id": 2,"parent_id": 1},{"id": 3,"parent_id": 2},{"id": 7,"parent_id": 3},{"id": 67,"parent_id": 1},] data.sort(byHierarchy); var dest = [], idx = {}; for (var i of data) { if (i.parent_id === 1) { dest.push(i); } else { let j = idx[i.parent_id]; if (j.child) j.child.push(i); else j.child = [i]; } idx[i.id] = i; } function byHierarchy(a, b) { if (a.parent_id === b.parent_id) return a.id - b.id; return a.parent_id - b.parent_id; } console.log(JSON.stringify(dest, null, 2).replace(/([{[\]}])[\s\n]+([{[\]}])/g, '$1$2'));

I think this is the cleanest way to do it我认为这是最干净的方法

 const data=[{"id": 2,"parent_id": 1},{"id": 3,"parent_id": 2},{"id": 7,"parent_id": 3},{"id": 67,"parent_id": 1},] let makeTree = (data, parent) => { let node = [] data.filter(d => d.parent_id == parent).forEach(d => { d.child = makeTree(data, d.id) node.push(d) }) return node } console.log(JSON.stringify(makeTree(data,1),null,2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM