Help to format data
Hi, I have an array like this:
[
{file: /file1.txt, otherinfo: foo},
{file: /path/file2.txt, otherinfo: foo},
{file: /path/file3.txt, otherinfo: foo},
{file: /path/subpath/file.txt, otherinfo: foo},
{file: /file4.txt, otherinfo: foo},
{file: /folder/dir/file5.txt, otherinfo: foo}
]
How can I format data to passing it to react-treebeard?
Thanks
From the README file of the project, the shape of your data should look like this:
{
id: '[optional] string',
name: 'string',
children: '[optional] array',
toggled: '[optional] boolean',
active: '[optional] boolean',
loading: '[optional] boolean',
decorators: '[optional] object',
animations: '[optional] object'
},
where you can omit the [optional] keys.
If you want to have the file/folder structure as depicted by the file keys in your objects, following will work. However, you have a second key named otherinfo in each of those objects. Is that supposed to be some metadata associated with each of those nodes? It doesn't look like this component supports something like that. I may be wrong though, I just found this component and started playing with it recently.
const data = {
name: '/',
children: [
{ name: 'file1.txt' },
{
name: 'path',
children: [
{ name: "file2.txt" },
{ name: "file3.txt" },
{
name: "subpath",
children: [
{ name: "file.txt" }
]
}
]
},
]
},
{
name: "file4.txt"
},
{
name: "folder",
children: [
{ name: "dir",
children: [
{ name: "file5.txt" }
]
}
]
}
Thanks for your reply @kamalx My doubt is about how can I transform my structure in react-treebeard shape. I receive the list of files from an API, so after fetching I need to transform it
otherinfo metadata key is not a problem. I have already tried to add it in a static example and works.