I have some issues converting a flat list to hierarchical.
Im trying to visualize d3.js tree chart from below data.
Each node has two child objects described in fams
array.
I found something similar to my problem here, but I can't get it working with two parent nodes Converting flat structure to hierarchical
This is what i want to archive https://bl.ocks.org/d3noob/8329404
I hope someone can point me in the right direction, thanks in advance.
let nodes = [
{ id: 'N001', fam: 'F001' },
{ id: 'N002', fam: 'F002' },
{ id: 'N003', fam: 'F003' },
{ id: 'N004', fam: 'F004' },
{ id: 'N005', fam: 'F005' },
{ id: 'N006', fam: 'F006' },
{ id: 'N007', fam: 'F007' },
{ id: 'N008', fam: 'F008' },
{ id: 'N009', fam: 'F009' },
{ id: 'N010', fam: 'F010' },
{ id: 'N011', fam: 'F011' }
];
let fams = [
{ id: 'F001', p1: 'N002', p2: 'N003' },
{ id: 'F002', p1: 'N004', p2: 'N005' },
{ id: 'F003', p1: 'N006', p2: 'N007' },
{ id: 'F004', p1: 'N008', p2: 'N009' },
{ id: 'F005', p1: 'N010', p2: 'N011' }
];
// Expected output
let finalData = [
{
id: 'N001',
children: [
{
id: 'N002',
fam: 'F002',
children: [
{
id: 'N004',
fam: 'F004',
children: [
{ id: 'N008', fam: 'F008' },
{ id: 'N009', fam: 'F009' }
]
},
{
id: 'N005',
fam: 'F005',
children: [
{ id: 'N010', fam: 'F010' },
{ id: 'N011', fam: 'F011' }
]
}
]
},
{
id: 'N003',
fam: 'F003',
children: [
{ id: 'N006', fam: 'F006' },
{ id: 'N007', fam: 'F007' }
]
}
]
}
];