When I call the function buildFileTree
, I store its response in a constant variable called data.
const data = this.buildFileTree(dataObject, 0);
The value of dataObject
is:
const dataObject = JSON.parse(TREE_DATA);
And the content of TREE_DATA
is:
const TREE_DATA = JSON.stringify([
{
Standard: "Food",
Category: [
{
Name: "Vegetable",
Tables: [
{
Description:
"The carrot is a simple root vegetable, usually conical or cylindrical in shape.",
Name: "Carrots"
},
{
Description:
" tomatoes come in a wide variety of shapes: round, oblate, pear-shaped, torpedo-shaped,",
Name: "Tomatoes"
}
]
},
{
Name: "Fruits",
Tables: [
{
Description: "Oranges",
Name: "Spherical shape is of orange"
},
{
Description: "Grapes",
Name:
"Grapes are typically an ellipsoid shape resembling a prolate spheroid."
}
]
}
]
}
]);
The buildFileTree function is defined as follows:
buildFileTree(obj: { [key: string]: any }, level: number): FileNode[] {
return Object.keys(obj).reduce<FileNode[]>((accumulator, key) => {
const value = obj[key];
const node = new FileNode();
node.filename = key;
if (value != null) {
if (typeof value === "object") {
node.children = this.buildFileTree(value, level);
} else {
node.type = value;
}
}
return accumulator.concat(node);
}, []);
}
}
The output generated from the function execution is displayed as:
Desired Result : Format
It appears that modifications may be necessary in the buildFileTree function. Can someone assist me with this matter?
A live example showcasing the current response can be accessed here: https://stackblitz.com/edit/angular-qsb9c8-x4oaan?file=app%2Ftree-nested-overview-example.ts