result = [ { cid: 59, pid: 0, name: ‘PHP’ }, { cid: 19, pid: 0, name: ‘C++’ }, { cid: 20, pid: 19, name: ‘STL’ }, { cid: 21, pid: 19, name: ‘MFC’ }, { cid: 60, pid: 59, name: ‘Zend Framework’ } ];
function test(result, pid) { rtn = []; for(i in result) { if(result[i].pid == pid) { result[i].children = test(result, result[i].cid); rtn.push(result[i]); } } return rtn; }
tree = test(result, 0); console.log(tree); // 结果却是下边这样 [ { cid: 60, pid: 59, name: 'Zend Framework’, children: [ [Circular], [Circular] ] }, { cid: 60, pid: 59, name: 'Zend Framework’, children: [ [Circular], [Circular] ] } ]
由test递归调用将result元数据组成树状,可是结果却不正确,请教!!!,还有,其中children里的值是“Circular”是什么意思?