在javascript中从平面列表构造树

在javascript中从平面列表构造树,javascript,Javascript,我有一个简单的列表,如下所示 flatList = [ { id: "39000000", parent: null, description: "Electric Systems", name: "39000000" }, { id: "39120000", parent: "39000000", description: "Electrical E

我有一个简单的列表,如下所示

flatList = [
    {
        id: "39000000", 
        parent: null, 
        description: "Electric Systems", 
        name: "39000000"
    },
    {
        id: "39120000", 
        parent: "39000000", 
        description: "Electrical Equipment", 
        name: "39120000"
    },
    {
        id: "39121000", 
        parent: "39120000", 
        description: "Power Conditioning", 
        name: "39121000"
    },  
    {
        id: "39121011", 
        parent: "39121000", 
        description: "Uninterruptible Power Supply", 
        name: "39121011"
    }
];
方法从平面列表构造树并将树存储在nestedList中

nestedList = [];

getTree(flatList) {
    flatList.forEach(element => {
      if (!element.parent) {
        this.nestedList.push({ id: element.id, name: element.name, description: element.description, children: [] });
      } else {
        if (this.nestedList.findIndex(item => item.id === element.parent) === -1) {
          this.nestedList.push({
            id: element.id, name: element.name, description: element.description, children: [{ id: element.id, name: element.name, description: element.description, children: [] }]
          });
        } else {
          this.nestedList.find(item => item.id === element.parent).children.push(
            { id: element.id, name: element.name, description: element.description, children: [] }
          );
        }
      }
    });
  }
我得到的输出如下所示

nestedList = [
    {
        id: "39000000", 
        name: "39000000",  
        description: "Electric Systems", 
        children: [{
            id: "39120000", 
            name: "39120000", 
            description: "Electrical Equipment", 
            children: []}
        ]
    },
    {
        id: "39121000", 
        name: "39121000", 
        description: "Power Conditioning", 
        children: [{
                id: "39121000", 
                name: "39121000", 
                description: "Power Conditioning", 
                children: []
            },
            {
                id: "39121011", 
                name: "39121011", 
                description: "Uninterruptible Power Supply", 
                children: []
            }       
        ]
    }   
]
所需输出应为:

    nestedList = [
    {
      id: "39000000",
      name: "39000000",
      description: "Electric Systems",
      children: [{
        id: "39120000",
        name: "39120000",
        description: "Electrical Equipment",
        children: [{
          id: "39121000",
          name: "39121000",
          description: "Power Conditioning",
          children: [{
            id: "39121011",
            name: "39121011",
            description: "Uninterruptible Power Supply",
          }]
        }]
      }]
    }
  ]

非常感谢您的帮助。

试试这个功能,希望它能帮助您

flatList=[
{
id:“39000000”,
父项:null,
说明:“电气系统”,
名称:“39000000”
},
{
id:“39120000”,
家长:“39000000”,
说明:“电气设备”,
姓名:“39120000”
},
{
id:“39121000”,
家长:“39120000”,
描述:“功率调节”,
名称:“39121000”
},  
{
id:“39121011”,
家长:“39121000”,
说明:“不间断电源”,
姓名:“39121011”
}
];
函数getUnflatten(arry){
数据=arry.reduce(函数(r,a){
var指数=0,节点;
if(node&&Object.keys(node.length){
node.children=node.children | |[];
节点。子节点。推(a);
}否则{
while(索引控制台日志(树)以下是更现代的代码(解释如下):

const src=[
{
id:“39000000”,
父项:null,
说明:“电气系统”,
名称:“39000000”
},
{
id:“39120000”,
家长:“39000000”,
说明:“电气设备”,
姓名:“39120000”
},
{
id:“39121000”,
家长:“39120000”,
描述:“功率调节”,
名称:“39121000”
},  
{
id:“39121011”,
家长:“39121000”,
说明:“不间断电源”,
姓名:“39121011”
}
];
const findChildren=
(家长,参考阵列)=>
map({id,parent,description,name})=>
({id,description,name,children:findChildren(referenceArray.filter(i=>
i、 父项===id),src)})

log(findChildren(src.filter(i=>i.parent==null),src))
您只在嵌套列表中查找父级。但是父元素可以是嵌套列表中某个元素的子元素。使用映射,其中每个节点都由其ID索引。然后在此映射中找到每个元素的父元素,并将该元素添加到找到的父元素的子元素中。然后创建一个数组,其中包含贴图中没有任何父节点的所有节点。这将是正确的和更有效的。谢谢!但这与问题中所示的期望输出正好相反。你能检查一下吗?谢谢你的示例代码和解释。这对我来说非常有效:)@Samvid不客气!然而,在StackOverflow上,您通过向上投票并接受答案来表示感谢!是的,我知道。但是,我仍然在建立我的声誉,因此如果我投票,它不会公开反映!