I have a data structure that contains nested arrays - I want to display the the data to make it look like a file structure - so nested data is represented as nested files / folders. However on console logging what the recursive function returns does not represent the nested nature of the data structure.
Here is my data structure:
const data = {
"root": [
{
"type": "folder",
"name": "one",
"children": [
{
"type": "folder",
"name": "one",
"children": []
},
{
"type": "file",
"name": "two",
"extension": ".txt",
"preview": "photos.google.com/abc.jpg"
},
{
"type": "file",
"name": "four",
"extension": ".txt",
"preview": "photos.google.com/abc.jpg"
}
]
},
{
"type": "file",
"name": "two",
"extension": ".txt",
"preview": "photos.google.com/abc.jpg"
},
{
"type": "file",
"name": "three",
"extension": ".txt",
"preview": "photos.google.com/abc.jpg"
},
{
"type": "file",
"name": "four",
"extension": ".txt",
"preview": "photos.google.com/abc.jpg"
}
]
}
And my recursive function:
const recurse = (data, children = false) => {
data.forEach(object => {
if (object.type === 'folder') {
recurse(object.children, true)
}
if (children) {
console.log(' ' + object.type + ' ' + object.name);
} else {
console.log(object.type + ' ' + object.name);
}
})
}
recurse(data.root)
And the console log:
' folder one'
' file two'
' file four'
'folder one'
'file two'
'file three'
'file four'
So the function is printing from the inside out of the structure. What approach should I be using if I want to display it in a way that reflects the nested nature of the data structure?