function treeify(list, idAttr, parentAttr, childrenAttr) { if (!idAttr) idAttr = 'id'; if (!parentAttr) parentAttr = 'parent'; if (!childrenAttr) childrenAttr = 'children'; var treeList = []; var lookup = {}; list.forEach(function(obj) { lookup[obj[idAttr]] = obj; obj[childrenAttr] = []; }); list.forEach(function(obj) { if (obj[parentAttr] != null) { lookup[obj[parentAttr]][childrenAttr].push(obj); } else { treeList.push(obj); } }); return treeList;};
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)