jquery线性数据转化为树
jquery线性数据转化为树
代码:
// data:数组
// parent_id:主键ID
function toTree(data, parent_id) {
var tree = [];
var temp;
for(var i = 0; i < data.length; i++) {
if(data[i].pid == parent_id) {
var obj = data[i];
temp = toTree(data, data[i].id);
if(temp.length > 0) {
obj.childrens = temp;
} else {
obj.childrens = [];
};
tree.push(obj);
};
};
return tree;
};