首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

js中把jsonAarray递归的步骤转成树形的json对象

2013-09-06 
js中把jsonAarray递归的方法转成树形的json对象现在在js中我获得了一个json 数组var nodes [{id:2,ti

js中把jsonAarray递归的方法转成树形的json对象
现在在js中我获得了

一个json 数组

var nodes = [
{"id":2,"title":"第一级1","parentid":0},
{"id":3,"title":"第二级1","parentid":2},
{"id":4,"title":"第二级2","parentid":2},
{"id":5,"title":"第三级1","parentid":4},
{"id":6,"title":"第三级2","parentid":3}
];
如何用递归的方式

转成

var newnode = {
                id:"2",
                title:"第一级1",
               children:[
                           {
                             id:"3",
                             title:"第二级1",
                             children:[
                                        {
                                            id:"6",
                                            title:"第三级2",
                                            children:[]
                                        }
                                        ]


                            },
                           {
                             id:"4",
                             title:"第二级2",
                             children:[
                                         {
                                            id:"5",
                                            title:"第三级1",
                                            children:[]
                                         }
                                        ]
                            }
                         ]
              }

分享到:
[解决办法]
结贴吧,分给我,反正也没其他回答

热点排行