日期:2014-05-17  浏览次数:20721 次

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:[]
                                        }
                                        ]