{"ScriptPreparationCode":"var data = [\r\n { name: \u0027john\u0027, savings: 900, firm: \u0027chase\u0027, advisor: \u0027holly\u0027 },\r\n {\r\n name: \u0027carol\u0027,\r\n savings: 500,\r\n checking: 600,\r\n firm: \u0027chase\u0027,\r\n advisor: \u0027mark\u0027,\r\n },\r\n {\r\n name: \u0027sarah\u0027,\r\n savings: 500,\r\n checking: 300,\r\n firm: \u0027chase\u0027,\r\n advisor: \u0027holly\u0027,\r\n },\r\n { name: \u0027toby\u0027, savings: 500, checking: 300, firm: \u0027chase\u0027, advisor: \u0027mark\u0027 },\r\n {\r\n name: \u0027casey\u0027,\r\n savings: 500,\r\n checking: 200,\r\n firm: \u0027citiBank\u0027,\r\n advisor: \u0027cindy\u0027,\r\n },\r\n { name: \u0027bruce\u0027, checking: 200, firm: \u0027chase\u0027, advisor: \u0027holly\u0027 },\r\n { name: \u0027jim\u0027, checking: 200, firm: \u0027chase\u0027, advisor: \u0027holly\u0027 },\r\n { name: \u0027nich\u0027, firm: \u0027chase\u0027, advisor: \u0027mark\u0027 },\r\n];","TestCases":[{"Name":"n","Code":" var keys = [\r\n [\u0027firm\u0027, \u0027advisors\u0027],\r\n [\u0027advisor\u0027, \u0027clients\u0027],\r\n ];\r\n var result = data.reduce(\r\n (r, o) =\u003E {\r\n keys\r\n .reduce((q, [key, children]) =\u003E {\r\n let name;\r\n ({ [key]: name, ...o } = o);\r\n if (!q[name]) {\r\n q[name] = { _: [] };\r\n q._.push({ name, [children]: q[name]._ });\r\n }\r\n return q[name];\r\n }, r)\r\n ._.push(o);\r\n return r;\r\n },\r\n { _: [] }\r\n )._;","IsDeferred":false},{"Name":"b","Code":"function group(arr, accumulate) {\r\n let accumulator = {};\r\n\r\n for (const obj of arr) {\r\n accumulator = accumulate(accumulator, obj);\r\n }\r\n\r\n return Object.values(accumulator);\r\n}\r\n\r\nfunction by(parentProp, childProp) {\r\n return function (acc, obj) {\r\n const { [parentProp]: name, ...child } = obj;\r\n (acc[name] ??= { name, [childProp]: [] })[childProp].push(child);\r\n\r\n return acc;\r\n };\r\n}\r\n\r\nvar result = group(data, by(\u0027firm\u0027, \u0027advisors\u0027)).map(\r\n ({ advisors, ...firm }) =\u003E ({\r\n ...firm,\r\n advisors: group(advisors, by(\u0027advisor\u0027, \u0027clients\u0027)),\r\n })\r\n);","IsDeferred":false}]}