My CSV file contains hierarchical data, with different levels separated by semicolons and values separated by commas. Here's an example:
Parent1;Child1;Grandchild1;3;3,5
Parent1;Child1;Grandchild2;3;3,5
Parent2;Child2;Grandchild2;4,4
Parent3;Child4;Grandchild1;5,5
I want to use javascript/jQuery/d3.js to transform this data into a multi-dimensional array like this:
var tree = [
{
text: "Parent 1",
nodes: [
{
text: "Child 1",
nodes: [
{
text: "Grandchild 1"
},
{
text: "Grandchild 2"
}
]
},
{
text: "Child 2"
}
]
},
{
text: "Parent 2"
},
{
text: "Parent 3"
}
];
The challenge lies in recursively looping through the data and ensuring that the values do not become part of the multidimensional array.
Here is my current code, which is not functioning as intended:
function createNavi(jsonRoot){
function printParentAddChildren(parent){
var parentObj = {};
parentObj.text = parent.name;
parentObj.backColor = colors[parent.depth];
var children = parent.children;
if(children==null) return parentObj;
parentObj.node = [];
for(var i=0; i<children.length; i++){
parentObj.node.push(printParentAddChildren(children[i]));
}
return parentObj;
}
var tree = [];
var children = jsonRoot.children;
for(var i=0; i<children.length; i++){
tree.push(printParentAddChildren(children[i]));
}
$('#tree').treeview({
data: tree
});
console.log(tree);
};
Any assistance on this would be greatly appreciated. Thank you!