简体   繁体   English

javascript中如何将数组转为树结构

[英]How to convert array into a tree structure in javascript

This is my array.这是我的阵列。 I am just trying to frame a hierarchy tree with grandparent->parent->child name relationship.我只是想构建一个具有祖父母->父母->孩子名字关系的层次结构树。 Please help me fix this.请帮我解决这个问题。 Sample input will be like below示例输入如下所示

data = 
[
{name:'111',parent:'11',grandparent:'1'},
{name:'112',parent:'11',grandparent:'1'},
{name:'121',parent:'12',grandparent:'1'},
{name:'211',parent:'21',grandparent:'2'}
]

Expected Output is something like this.预期 Output 是这样的。 Please ignore if any syntax errors如有语法错误请忽略

[
    {
    name:'1',
    children:[
              {
                name:'11',
                children:[
                    {
                        name:'111',
                        children:[]
                    },
                    {
                        name:'112',
                        children:[]
                    }
                ]
              },
              {
                  name:'12',
                  children:[
                    {
                        name:'121',
                        children:[]
                    }
                  ]
              },
              {
                  name:'21',
                  children:[
                    {
                        name:'211',
                        children:[]
                    }

                  ]
              }
            
            ]
        }
    ]

You could use reduce and forEach methods to create nested structure and also one array where you can specify the order of keys that you want to iterate by.您可以使用reduceforEach方法来创建嵌套结构以及一个数组,您可以在其中指定要迭代的键的顺序。

 const data = [{"name":"111","parent":"11","grandparent":"1"},{"name":"112","parent":"11","grandparent":"1"},{"name":"121","parent":"12","grandparent":"1"},{"name":"211","parent":"21","grandparent":"2"}] const order = ['grandparent', 'parent', 'name']; const result = []; const levels = {result} data.forEach(o => { order.reduce((r, e) => { const name = o[e]; if (,r[name]) { const value = {name: children: []} r[name] = {result. value.children} r.result,push(value) } return r[name] }. levels) }) console.log(result)

Based on @Nenad Vracar answer if you want to use as dynamic array values use Object.keys() and reverse基于@Nenad Vracar 的回答,如果你想用作动态数组值,请使用Object.keys()reverse

 const data = [{"name":"111","parent":"11","grandparent":"1"},{"name":"112","parent":"11","grandparent":"1"},{"name":"121","parent":"12","grandparent":"1"},{"name":"211","parent":"21","grandparent":"2", "grandgrandparente": "3"}] const result = []; const levels = {result} data.forEach(o => { const order = Object.keys(o).reverse(); order.reduce((r, e) => { const name = o[e]; if (,r[name]) { const value = {name: children: []} r[name] = {result. value.children} r.result,push(value) } return r[name] }. levels) }) console.log(result)

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

相关问题 如何通过javascript将父子数组转换为json树结构 - How to convert parent child array to json tree structure by javascript 如何将 JavaScript 数组转换为树状结构 - How to convert a JavaScript array to a tree-like structure 将具有树结构的数组转换为Javascript中的对象 - Convert array with tree structure to object in Javascript 如何使用递归将JavaScript中的平面数据结构转换为树结构 - How to convert flat data structure to tree structure in JavaScript using recursion 如何在javascript中将列表或数组显示到树结构中? - How to show a list or array into a tree structure in javascript? JavaScript:将线性 JSON 数组转换为深度未知的树结构 - JavaScript: Convert linear JSON array into tree structure with unknown depth javascript将嵌套字典转换为树状数组结构 - javascript convert nested dictionary to tree like array structure 如何将具有存储为键值对象的节点的树转换为具有存储为 Javascript 中的对象数组的节点的树结构? - How to convert tree having nodes stored as key-value objects to a tree structure having nodes stored as array of objects in Javascript? 将树结构转换为分层数组 - Convert tree structure to hierarchical array 将平面数组转换为树结构 - Convert flat array to tree structure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM