I am faced with the challenge of creating a tree Array that can be used in the primeng tree component. However, I am receiving a flat array from the backend.
Here is an example of the data I receive:
{name: 'teste', previousName: 'fathername', showOrder: '1', preferredOrder: '2'},
{name: 'teste 2', previousName: 'fathername', showOrder: '1', preferredOrder: '2'},
{name: 'teste 3', previousName: 'teste', showOrder: '1', preferredOrder: '2'},
{name: 'teste 4', previousName: 'teste', showOrder: '1', preferredOrder: '2'},
{name: 'teste 5', previousName: 'teste 3', showOrder: '1', preferredOrder: '2'},
{name: 'teste 6', previousName: 'teste 5', showOrder: '1', preferredOrder: '2'},
]
and I need to transform it into:
[
{label: 'name', data: 'original object', draggable: true, droppable: true, children: []}
]
To achieve this, I attempted to create the desired objects using the following function:
makeTreeNode(array) {
let arrTreeNode = []
let treeNodeObj;
array.filter(element => {
treeNodeObj = new Tree() //Tree is a class with the desired properties
treeNodeObj.label = element.name
treeNodeObj.data = element
treeNodeObj.draggable = true
treeNodeObj.droppable = true
arrTreeNode.push(treeNodeObj)
})
This conversion works, but I am unsure of how to read this new array in order to match objects where the previous name matches the current name and then place them as children nodes.
If anyone has any insights or solutions, please help me out. Thank you!