CSV File Data:
parent_name, child_name
A1, A2
A1, A3
A1, A4
A1, A5
A2, A12
A2, A16
A2, A18
A2, A19
Output: Javascript Object to be made
{
name: A1,
children: [
{
name: A2,
children: [
{
name: A4,
children: []
}
]
},
{
name: A3,
children: []
}
}
Basically, I have to convert a CSV file to make a Tree in d3. I am wondering how to make the dictionary required from the CSV file
d3.csv("test1.csv").then(function (data) {
var dataset = {};
data.forEach(function (d,i){
console.log(d.parent_name, d.child_name);
if(i === 0){
dataset["name"] = d.parent_name;
dataset["children"] = [];
}
if (dataset["name"] === d.parent_name){
dataset.children.push(NodeMaker(d.child_name))
}
});
function NodeMaker(name){
return {"name": name, "children": []};
}
console.log(dataset);
});
So this is the code, I have which only make a dictionary-like, It doesn't go deeper than the 1st level of the root node
{
name: A1,
children: [
{
name: A2,
children: []
},
{
name: A3,
children: []
}
]
}
'A3'is going to?d3.stratify(). That's exactly what it's built for.