{"ScriptPreparationCode":"var arr = [];\r\nvar len = 1000;\r\nfunction init() {\r\nfor (var i = 0; i \u003C len; i\u002B\u002B) {\r\n arr.push({\r\n id: i\u002B1,\r\n name: i\u002B\u0022 ...\u0022,\r\n parent: i\r\n });\r\n}}\r\n\r\ninit();","TestCases":[{"Name":"@john36allTa","Code":"function pack( data ){\r\n const childs = id =\u003E \r\n data.filter( item =\u003E item.parent === id )\r\n .map( \r\n ({id,name}) =\u003E ({id,name, children: childs(id)}) \r\n ).map(\r\n ({id,name,children}) =\u003E children.length ? {id,name, children} : { id, name }\r\n );\r\n return childs(0);\r\n}\r\n\r\nconst res1 = pack(arr)","IsDeferred":false},{"Name":"@dimoff66","Code":"function pack( arr ) {\r\n const map = Object.assign({} , ...arr.map(v =\u003E \r\n ({ [v.id]: Object.assign(v, { children: [] }) })\r\n ))\r\n\r\n const tree = Object.values(map).filter(v =\u003E \r\n !(v.parent \u0026\u0026 map[v.parent].children.push(v))\r\n )\r\n \r\n return tree\r\n}\r\n\r\nconst res2 = pack(arr)","IsDeferred":false},{"Name":"@Bavashi","Code":"function makeTree(array, parent) {\r\n var tree = {};\r\n parent = typeof parent !== \u0027undefined\u0027 ? parent : {id: 0};\r\n\r\n var childrenArr = array.filter(function(child) {\r\n return child.parent == parent.id;\r\n });\r\n\r\n if (childrenArr.length \u003E 0) {\r\n var childrenObj = {};\r\n childrenArr.forEach(function(child) {\r\n childrenObj[child.id] = child;\r\n });\r\n if (parent.id == 0) {\r\n tree = childrenObj;\r\n } else {\r\n parent.children = childrenObj;\r\n }\r\n childrenArr.forEach(function(child) {\r\n makeTree(array, child);\r\n })\r\n }\r\n\r\n return tree;\r\n};\r\n\r\nconst res3 = makeTree(arr);","IsDeferred":false}]}