I am trying to convert a flat array to an array of arrays since I will use the data in jsTree. I also need to convert key names such as "Name" to "text".
I want to use lodash.js, but I'm really new to lodash. I was looking for a solution, but could not find a suitable one for my case.
So can you help with this? The following are data with flat arrays:
[
{
Id:1,
Name: 'name1',
Parent: 0
},
{
Id:2,
Name: 'name2',
Parent: 1
},
{
Id:3,
Name: 'name3',
Parent: 2
},
{
Id:4,
Name: 'name4',
Parent: 1
},
{
Id:5,
Name: 'name5',
Parent: 1
},
{
Id:6,
Name: 'name6',
Parent: 5
}
]
I would like to have tree data such as:
{
"id": 1,
"text" : "name1",
"children" : [
{
"id": 2,
"text" : "name2",
"children" : [{
"id": 3,
"text": "name3"
}]
},
{
"id": 4,
"text" : "name4"
},
{
"id": 5,
"text" : "name5",
"children" : [{
"id": 6,
"text": "name6"
}]
}
]
}
Thank you in advance