I have a JSON table data and want to convert to JSON tree data as shown below. I'm looking for an efficient algorithm in JavaScript using any new ECMAScript 6 operator or statements with a functional approach (not by standard recursive algorithm with ES5)?
Table data:
[
{
"Children":"4th Grand Father"
},
{
"Name":"4th Grand Father",
"Children":"3rd Grand Father"
},
{
"Name":"3rd Grand Father",
"Children":"2nd Grand Father"
},
{
"Name":"2nd Grand Father",
"Children":"Grand Father"
},
{
"Name":"Grand Father",
"Children":"Father"
},
{
"Name":"Grand Father",
"Children":"Uncle"
},
{
"Name":"Uncle",
"Children":"Cousin"
},
{
"Name":"Father",
"Children":"Brother"
},
{
"Name":"Father",
"Children":"Me"
}
]
Tree data:
[
{
"Name": "4th Grand Father",
"Children": [
{
"Name": "3rd Grand Father",
"Children": [
{
"Name": "2nd Grand Father",
"Children": [
{
"Name": "Grand Father",
"Children": [
{
"Name": "Father",
"children": [
{
"Name": "Brother"
},
{
"Name": "Me"
}
]
},
{
"Name": "Uncle",
"children": [
{
"Name": "Cousin"
}
]
}
]
}
]
}
]
}
]
}
]
Mapand two simple loops.