i trying transform nested object comprised of keys nested array of data comprised of data referenced keys, conforms d3.js schema bubblechart.
the nested object looks this:
{ "f5eade20-110f-11e5-9c61-33ffcb46c0ef":{ }, "ff4477e0-6a2c-11e4-afd1-83393415d9ef":{ "0153f8b0-265a-11e4-8585-9b3341099b01":{ "0159c510-265a-11e4-8585-9b3341099b01":{ "11615f20-fbf9-11e4-b3d5-771ea6705f5b":{ } }, "83180020-f504-11e4-917c-b121cbe20dd2":{ }, "ddc13830-fa76-11e4-a09d-430b71e0fc3f":{ } }, "022bbb10-265a-11e4-8585-9b3341099b01":{ "7daa5900-fbf6-11e4-bdf8-fdcd51308cde":{ }, "cbbad030-6c0d-11e4-b161-d9c9c481a5bc":{ "3990b5c0-f53e-11e4-917c-b121cbe20dd2":{ } }, "7347bc80-38fd-11e4-a2b7-d7a74243b515":{ "98b00f90-fb53-11e4-bd16-359096879648":{ } }, "a7dd2b50-36ce-11e4-b2d6-85c2a0c3f504":{ "7347bc80-38fd-11e4-a2b7-d7a74243b515":{ "f823a7b0-f753-11e4-b9ef-1bae8d43be66":{ } } } } } } after looking these keys in array value (name attribute), transform needs d3.js read it:
{ "name":"parent 1", "children":[ { "name":"child 1", "children":[ { "name":"child 1", "children":null }, { "name":"grandchild 1.1", "children":null }, { "name":"grandchild 1.2", "children":null } ] }, { "name":"child 2", "children":[ { "name":"child 2", "children":null }, { "name":"grandchild 2.1", "children":null }, { "name":"grandchild 2.2", "children":null } ] }, { "name":"child 3", "children":[ { "name":"child 3", "children":null }, { "name":"grandchild 3.1", "children":null }, { "name":"grandchild 3.2", "children":null } ] }, { "name":"child 4", "children":[ { "name":"child 4", "children":null }, { "name":"grandchild 4.1", "children":null }, { "name":"grandchild 4.2", "children":null } ] } ] } i've beat head against wall trying figure out how , grateful help.
you want use recursive function this. below basic stuff started.
var obj = { "f5eade20-110f-11e5-9c61-33ffcb46c0ef":{ }, "ff4477e0-6a2c-11e4-afd1-83393415d9ef":{ "0153f8b0-265a-11e4-8585-9b3341099b01":{ "0159c510-265a-11e4-8585-9b3341099b01":{ "11615f20-fbf9-11e4-b3d5-771ea6705f5b":{ } }, "83180020-f504-11e4-917c-b121cbe20dd2":{ }, "ddc13830-fa76-11e4-a09d-430b71e0fc3f":{ } }, "022bbb10-265a-11e4-8585-9b3341099b01":{ "7daa5900-fbf6-11e4-bdf8-fdcd51308cde":{ }, "cbbad030-6c0d-11e4-b161-d9c9c481a5bc":{ "3990b5c0-f53e-11e4-917c-b121cbe20dd2":{ } }, "7347bc80-38fd-11e4-a2b7-d7a74243b515":{ "98b00f90-fb53-11e4-bd16-359096879648":{ } }, "a7dd2b50-36ce-11e4-b2d6-85c2a0c3f504":{ "7347bc80-38fd-11e4-a2b7-d7a74243b515":{ "f823a7b0-f753-11e4-b9ef-1bae8d43be66":{ } } } } } }; var final_obj = convert(obj); function convert(obj_to_convert) { var new_obj = []; (var in obj_to_convert) { new_obj.push({ name: i, children: convert(obj_to_convert[i]) }); } return new_obj; }; $('#results').text(json.stringify(final_obj, null, 4)); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <pre id="results"></pre>
Comments
Post a Comment