i have object array data
const data = [{id: 1, name : "John", parentid: 1}, {id: 2, name "David", parentid: 1}, {id: 3, name "Gelly", parentid: 2}, {id: 4, name "Jolly", parentid: 3}, {id: 5, name "Natalie", parentid: 2}]
I need to convert this object array to single object which respects parentid using lodash( or something, must be very fast). How can I do this?
result: { "root": [
{
id: 1,
name : "John",
parentid: 1,
children: [
{
id: 2,
name "David",
parentid: 1,
children: [
{
id: 5,
name "Natalie",
parentid: 2},
{
id: 3,
name "Gelly",
parentid: 2,
children: [
{
id: 4,
name "Jolly",
parentid: 3
}
]
}
]
}]
}
]
}
I tried this
https://codesandbox.io/s/m7l42kv52j?file=/index.js
But doesn't worked for me.