frontend-collective / frontend-collective/react-sortable-tree
getTreeFromFlatData only supports self-referenced flat data form
- Dominant language
- JavaScript
- Stars
- 5k
- Forks
- 914
- PR merge metrics
- No merged PRs in 30d
Description
currently, the `getTreeFromFlatData` API only supports self-referenced flat data, for example, the following structure:
```
const initialData = [
{ id: '1', name: 'N1', parent: null },
{ id: '2', name: 'N2', parent: null },
{ id: '3', name: 'N3', parent: 2 },
{ id: '4', name: 'N4', parent: 3 },
];
```
Might be converted into a tree structure using the following mapping:
```
getTreeFromFlatData({
flatData: initialData.map(node => ({ ...node, title: node.name })),
getKey: node => node.id, // resolve a node's key
getParentKey: node => node.parent, // resolve a node's parent's key
rootKey: null, // The value of the parent key when there is no parent (i.e., at root level)
})
```
But it is not the case when using a flat associative data, like the following data for example:
```
var peopleData = [
{
'name': 'John Doe',
'age': 34,
'pets:name': 'Rex',
'pets:type': 'dog',
'pets:toys:type': 'bone'
},
{
'name': 'John Doe',
'age': 34,
'pets:name': 'Rex',
'pets:type': 'dog',
'pets:toys:type': 'ball'
},
{
'name': 'Mary Jane',
'age': 19,
'pets:name': 'Mittens',
'pets:type': 'kitten',
'pets:toys:type': 'yarn'
},
{
'name': 'Mary Jane',
'age': 19,
'pets:name': 'Fluffy',
'pets:type': 'cat'
}
];
```
How can I convert the above flat associative data to a well-supported tree structure?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.