递归
递归算法在日常工作中算是用的比较多的一种,比如DOM树的遍历,多层级树状结构的生成,遍历寻找某个树节点等
1 先来看下数据结构
var result = {
id:0,
name:"张飞",
item:[
{id:1,name:"关羽"},
{id:2,name:"刘备",item:[
{id:5,name:"荀彧"},
{id:6,name:"关平"}
]},
{id:3,name:"曹操"},
{id:4,name:"貂蝉"},
]
}
一般情况下后台返回类似于如上的嵌套数据结构,或者说只得到一部分数据,点击某个子节点,异步加载节点,异步加载之后的数据可能如下:
var result = {
id:0,
name:"张飞",
item:[
{id:1,name:"关羽"},
{id:2,name:"刘备",item:[
{id:5,name:"荀彧"},
{id:6,name:"关平"}
]},
//点击曹操这一项,加载出来刘禅和周仓,点击周仓,又异步加载项羽和别姬
{id:3,name:"曹操",item:[
{id:8,name:"刘禅"},
{id:9,name:"周仓",item:[
{id:10,name:"项羽