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}
];
function fn(data, pid) {
var result = [], temp;
for (var i = 0; i < data.length; i++) {
if (data[i].pid == pid) { //这个是一个个的与传入的进行匹配,匹配到了后,再执行
var obj = {"text": data[i].name,"id": data[i].id};
temp = fn(data, data[i].id);
if (temp.length > 0) { //生成最底层的数据结构的条件
obj.children = temp; //按最后的数据结构,然后汇总成大的数据结构
}
result.push(obj);
}
}
return result;
}