I have an csv file that contains an organization hierarchy data, one of the fields is the id of that employee's subordinates' ids.
The csv file is something looking like this:
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
The end result I am looking for is something like this:
{
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" }],
},
],
}
What i've tried so far: So far I've used csvtojson package to parse the csv file into an array, which gives me this:
[
{
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' }
]
I think some recursion is required to transform this data, but I am not super good with that, any help is appreciated!
Edit: Also I am flexible on how the columns look in csv, if there is anyway to make this transformation easier by alter the columns that is cool as well.
Edit: I am playing with the idea of having a report to field in the csv, I think this'll make it much easier;
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
Edit: after I change the csv columns I was able to come up with a solution. Gonna post it below in case other people are looking for this in the future.
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));
I am not even 100% sure how i got here, its just tons of trials. It seems to be working but if someone notice any bugs please point out. Also thanks a lot for the different approaches. I am going to learn them all! I am a newbie on stackoverflow and this is a warm welcome!