javascript - 从对象数组创建 PrimeNG TreeTable json

标签 javascript arrays json typescript primeng

我有一个已排序的对象数组。它们不通过 ID 相互连接,仅通过文本连接。

let result = [
    { col1: "A" , col2: "a" , col3: "1", col4: [10,15] },
    { col1: "A" , col2: "c" , col3: "1", col4: [30,35] },
    { col1: "B" , col2: "b" , col3: "1", col4: [20,25] },
    { col1: "C" , col2: "d" , col3: "1", col4: [40,45] },
    { col1: "C" , col2: "d" , col3: "2", col4: [50,55] },
    { col1: "D" , col2: "e" , col3: "3", col4: [60,65] },
    { col1: "D" , col2: "e" , col3: "4", col4: [70,75] },
    { col1: "D" , col2: "f" , col3: "1", col4: [80,85] }
]

我需要从此数组中绘制以下 TreeTable: enter image description here

The plunker link

因此,结果数组将转换为:

this.data = [
    {
        // COL1
        data:{
            label:"A"
        },
        children:[
            {
                // COL2
                data:{
                    label: "a"
                },
                children:[
                    {
                        // COL3
                        data:{
                            label: "1",
                            clusters: [10,15]
                        }
                    }
                ]
            },
            {
                // COL2
                data:{
                    label: "c"
                },
                children:[
                    {
                        // COL3
                        data:{
                            label: "1",
                            clusters: [30,35]
                        }
                    }
                ]
            }
        ]
    },
    {
        // COL1
        data:{
            label:"B"
        },
        children:[
            {
                // COL2
                data:{
                    label: "b"
                },
                children:[
                    {
                        // COL3
                        data:{
                            label: "1",
                            clusters: [20,25]
                        }
                    }
                ]
            }
        ]
    },
    {
        // COL1
        data:{
            label:"C"
        },
        children:[
            {
                // COL2
                data:{
                    label: "d"
                },
                children:[
                    {
                        // COL3
                        data:{
                            label: "1",
                            clusters: [40,45]
                        }
                    },
                    {
                        // COL3
                        data:{
                            label: "2",
                            clusters: [50,55]
                        }
                    }
                ]
            }
        ]
    },
    {
        // COL1
        data:{
            label:"D"
        },
        children:[
            {
                // COL2
                data:{
                    label: "e"
                },
                children:[
                    {
                        // COL3
                        data:{
                            label: "3",
                            clusters: [60,65]
                        }
                    },
                    {
                        // COL3
                        data:{
                            label: "4",
                            clusters: [70,75]
                        }
                    }
                ]
            },
            {
                // COL2
                data:{
                    label: "f"
                },
                children:[
                    {
                        // COL3
                        data:{
                            label: "3",
                            clusters: [80,85]
                        }
                    }
                ]
            }
        ]
    }
];

我尝试了一些方法,例如对结果数组进行分组,只是对结果数组进行循环...但我无法解决这个问题并最终迷失了。

你能帮我从结果数组创建数据数组吗?

最佳答案

试试这个:

let data = [];
result.forEach((r, i) => {
  let d;
  for (let i = 1; i <= 3; i ++) {
    const l = r["col"+i];
    const rg = d ? (d.children || (d.children = [])) : data;
    d = rg.find(e => e.data.label === l) || (rg[rg.length] = { data: { label: l }});
  }
  d.data.clusters = r["col4"];
});
console.log(data);

关于javascript - 从对象数组创建 PrimeNG TreeTable json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46332430/

相关文章:

java - 分而治之 - 未排序数组的 k 元素

javascript - 使用 hmac 和 sha256 的 Python 加密不会给出与 javascript 相同的结果

javascript - 单选按钮获取值

javascript - 强制 Javascript 函数调用调用 Function.prototype.call 方法

ios - 如果 Set 包含它,则更改数组内所有元素的变量

c++ - 在 C++ 中应该以什么顺序释放内存?

javascript - 加载多个 json 文件并使用 ajax 和 jquery 将其显示到 html

python - 如何在与 dictwriter 一起使用时正确删除 python 中的回车符(换行符 ='' 没有帮助)

c# - Javascript确认消息问题

javascript - 如何使用位置 :relative DIV act as a 'spacer' for other divs?