Consider this JSON data containing comments that are fetched via AJAX call:
json = 'comments': [
{'id':1,'parent':0},
{'id':2,'parent':1},
{'id':3,'parent':2},
{'id':4,'parent':0}]
In order to display them properly, they need to be structured as shown below:
target_object= comments: [
{id:1,parent:0, children:[
{id:2,parent:1, children: [
{id:3,parent:2}]}]},
{id:4,parent:0, children:[]}]
Query:
- What is the most effective method to achieve this structure? (Preferably using CoffeScript iterators, but JQuery/pure JS solutions are also acceptable).