我有一个包含组织层次结构数据的 csv 文件,其中一个字段是该员工下属 id 的 id。
csv 文件看起来像这样:
id,name,title,children
n1,Lao Lao,general manager,"n2,n3,n9,n10"
n2,Bo Miao,department manager
n3,Su Miao,department manager,"n4,n5,n8"
n4,Tie Hua,senior engineer
n5,Hei Hei,senior engineer,"n6,n7"
n6,Dan Dan,engineer
n7,Xiang Xiang,engineer
n8,Pang Pang,senior engineer
n9,Hong Miao,department manager
n10,Chun Miao,department manager,"n11"
n11,Yue Yue,senior engineer
我正在寻找的最终结果是这样的:
{
id: "n1",
name: "Lao Lao",
title: "general manager",
children: [
{ id: "n2", name: "Bo Miao", title: "department manager" },
{
id: "n3",
name: "Su Miao",
title: "department manager",
children: [
{ id: "n4", name: "Tie Hua", title: "senior engineer" },
{
id: "n5",
name: "Hei Hei",
title: "senior engineer",
children: [
{ id: "n6", name: "Dan Dan", title: "engineer" },
{ id: "n7", name: "Xiang Xiang", title: "engineer" },
],
},
{ id: "n8", name: "Pang Pang", title: "senior engineer" },
],
},
{ id: "n9", name: "Hong Miao", title: "department manager" },
{
id: "n10",
name: "Chun Miao",
title: "department manager",
children: [{ id: "n11", name: "Yue Yue", title: "senior engineer" }],
},
],
}
到目前为止我已经尝试过:到目前为止,我已经使用 csvtojson 包将 csv 文件解析为一个数组,这给了我这个:
[
{
id: 'n1',
name: 'Lao Lao',
title: 'general manager',
children: 'n2,n3,n9,n10'
},
{ id: 'n2', name: 'Bo Miao', title: 'department manager' },
{
id: 'n3',
name: 'Su Miao',
title: 'department manager',
children: 'n4,n5,n8'
},
{ id: 'n4', name: 'Tie Hua', title: 'senior engineer' },
{
id: 'n5',
name: 'Hei Hei',
title: 'senior engineer',
children: 'n6,n7'
},
{ id: 'n6', name: 'Dan Dan', title: 'engineer' },
{ id: 'n7', name: 'Xiang Xiang', title: 'engineer' },
{ id: 'n8', name: 'Pang Pang', title: 'senior engineer' },
{ id: 'n9', name: 'Hong Miao', title: 'department manager' },
{
id: 'n10',
name: 'Chun Miao',
title: 'department manager',
children: 'n11'
},
{ id: 'n11', name: 'Yue Yue', title: 'senior engineer' }
]
我认为转换这些数据需要一些递归,但我对此并不擅长,感谢任何帮助!
编辑:此外,我对列在 csv 中的外观很灵活,如果无论如何通过更改也很酷的列来使这种转换更容易的话。
编辑:我正在考虑在 csv 中生成一份报告,我认为这会更容易;
id,name,title,report_to
n1,Lao Lao,general manager
n2,Bo Miao,department manager,n1
n3,Su Miao,department manager,n1,
n4,Tie Hua,senior engineer,n3
n5,Hei Hei,senior engineer,n3,
n6,Dan Dan,engineer,n5
n7,Xiang Xiang,engineer,n5
n8,Pang Pang,senior engineer,n3
n9,Hong Miao,department manager,n1
n10,Chun Miao,department manager,n1
n11,Yue Yue,senior engineer,n10
编辑:在我更改了 csv 列之后,我能够想出一个解决方案。会把它贴在下面,以防其他人在未来寻找这个。
const source = await CSVToJSON().fromFile("./example.csv");
const dataForOrgChart = source
.map((employee) => {
employee.children = source.filter(
(child) => child.report_to === employee.id
);
return employee;
})
.map((employee) => {
delete employee.report_to;
return employee;
})
.filter(
(employee) =>
!source
.map((employee) => employee.children.map((child) => child.id))
.reduce((acc, cur) => [...acc, ...cur], [])
.includes(employee.id)
)[0];
console.log(JSON.stringify(dataForOrgChart));
我什至不能 100% 确定我是如何来到这里的,这只是大量的试验。它似乎正在工作,但如果有人发现任何错误,请指出。也非常感谢不同的方法。我要全部学习!我是stackoverflow的新手,热烈欢迎!