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;};小提琴



