javascript - Flattening deeply nested array of objects -
i got following data structure, array of accounts objects, accounts being parents children accounts, in turn can parents other accounts etc.:
[{ "id": "acc.1260446672222.11", "type": "expenses_folder", "name": "expense group", "balance": 3418.11, "children": [{ "id": "acc.1260446672238.27", "type": "expense", "name": "advertising, promotion , entertainment account", "balance": 0, "children": [] }, { "id": "acc.9a2492ba-0d82-4f4a-a1b4-14868f1e1a39", "type": "expenses_folder", "name": "premises costs", "balance": 0, "children": [{ "id": "acc.287ba5b6-5536-428b-950f-d71d2af73ccc", "type": "expense", "name": "use of home - gas", "balance": 0, "children": [ ] }, { "id": "acc.7091ee15-3f02-4bd1-94e5-5918cf986969", "type": "expense", "name": "hire of venue, studios, teaching rooms", "balance": 0, "children": [ ] }] }, { "id": "acc.827ec446-edeb-4f2b-8032-d306292d2d83", "type": "expenses_folder", "name": "administrative expenses", "balance": 558.61, "children": [{ "id": "acc.0ed5fc81-7734-4452-86a9-db22a6b0f8e8", "type": "expense", "name": "bank charges", "balance": 15, "children": [ ] }, { "id": "acc.e2cdb2c0-8565-4991-a35a-d4596b0ddf45", "type": "expense", "name": "software & computer peripherals", "balance": 417.13, "children": [ ] }, { "id": "acc.96d5d00e-43f4-4d3a-b97b-fdf258c65514", "type": "expense", "name": "printing, photocopying etc", "balance": 55.93, "children": [ ] }, { "id": "acc.494dd64a-4fb3-42b8-be3e-8f3b59a2ef59", "type": "expense", "name": "artists administration service", "balance": 0, "children": [ ] }, { "id": "acc.1260446672238.35", "type": "expense", "name": "stationery", "balance": 0, "children": [ ] }, { "id": "acc.96d89d0d-5465-488b-b37f-d41ca114c5e6", "type": "expense", "name": "mobile telephone", "balance": 41.19, "children": [ ] }, { "id": "acc.1260446672238.33", "type": "expense", "name": "home telephone", "balance": 0, "children": [ ] }, { "id": "acc.1260446672238.38", "type": "expense", "name": "postage/delivery", "balance": 29.36, "children": [ ] }] }, { "id": "acc.b9c9bbc7-43df-472e-9ac8-c7c76f08f49a", "type": "expenses_folder", "name": "instruments, equipment maintenance etc", "balance": 1002.48, "children": [{ "id": "acc.1260446672238.32", "type": "other_expenses", "name": "instrument insurance", "balance": 157.48, "children": [ ] }, { "id": "acc.2a1cca15-2868-4770-a3e7-d43a6268c6a1", "type": "expense", "name": "instrument repairs & maintenance", "balance": 845, "children": [ ] }, { "id": "acc.a908aee0-84fb-450a-916b-4cec25265aef", "type": "expense", "name": "accessories & replacement parts", "balance": 0, "children": [ ] }] }, { "id": "acc.a42cdd86-0d9e-4f3f-af0d-7c4525374731", "type": "expenses_folder", "name": "motor vehicle", "balance": 0, "children": [{ "id": "acc.cb325e7e-0ce4-4c78-9cb4-20659df733a6", "type": "expense", "name": "fuel , oil", "balance": 0, "children": [ ] }] }, { "id": "acc.4bdd9e26-ce64-4e7f-b46a-82ec9de06ded", "type": "expenses_folder", "name": "other travel", "balance": 132.1, "children": [{ "id": "acc.77dd2142-f2de-4a2c-9247-061d0661bc0a", "type": "expense", "name": "taxis", "balance": 24.5, "children": [ ] }, { "id": "acc.2b54abdd-7ef5-43cd-bdb9-c8c981b59ff2", "type": "expense", "name": "public transport", "balance": 107.6, "children": [ ] }] }, { "id": "acc.e4695b70-31fa-4e23-afd0-97335dcd5b9e", "type": "expense", "name": "subsitence", "balance": 0, "children": [ ] }, { "id": "acc.02d222bf-4dff-4308-afe9-69b93f412ada", "type": "expense", "name": "hotel , accomodation", "balance": 0, "children": [ ] }, { "id": "acc.d61cd5b4-2c80-4ab8-93d0-9d5726bd253b", "type": "expenses_folder", "name": "fees , commission paid", "balance": 0, "children": [{ "id": "acc.1262189019758.7", "type": "expense", "name": "pupils exam entry fees", "balance": 0, "children": [ ] }, { "id": "acc.a7d7efd3-d0da-4704-babb-079b6077f3fe", "type": "expense", "name": "audition, competition entry fees", "balance": 0, "children": [ ] }, { "id": "acc.3b91ee4e-40a8-46d8-aa05-3afa5974b3ef", "type": "expense", "name": "deputies, other musicians", "balance": 0, "children": [ ] }] }, { "id": "acc.250d6872-6023-4599-a0b6-b7159eebbfa1", "type": "expenses_folder", "name": "other professional expenses", "balance": 1739.42, "children": [{ "id": "acc.b7315228-f85a-4ffb-9199-d1128a409e5f", "type": "expense", "name": "promotion & publicity", "balance": 138.6, "children": [ ] }, { "id": "acc.69ca2005-d7a0-448b-b70c-dafb128a48ae", "type": "expense", "name": "other expenses", "balance": 364.5, "children": [ ] }, { "id": "acc.dcd999d2-4e18-41be-b9cc-218d4034b88e", "type": "expense", "name": "office equipment, furniture", "balance": 0, "children": [ ] }, { "id": "acc.e0460706-d5c9-4c40-9d1e-0d2058864b92", "type": "expense", "name": "cds, dowloads etc", "balance": 67.57, "children": [ ] }, { "id": "acc.1866df79-9e44-459a-a978-727904987469", "type": "expense", "name": "professional books, magazines", "balance": 104.01, "children": [ ] }, { "id": "acc.24c1651d-e7ae-48bc-a32d-311427e0fcea", "type": "expense", "name": "professional associations", "balance": 272.17, "children": [ ] }, { "id": "acc.289ab0ac-b9d3-435e-ac82-9da9702b7d4b", "type": "expense", "name": "tuition", "balance": 470, "children": [ ] }, { "id": "acc.f24cf99b-6291-4b9f-821e-425f4909d4e1", "type": "expense", "name": "scores, manuscript paper etc", "balance": 215.32, "children": [ ] }, { "id": "acc.1af95953-56f0-455e-9d0a-7c4e0477cf0d", "type": "expense", "name": "performance clothing", "balance": 0, "children": [ ] }, { "id": "acc.c0585577-535a-4ae2-a02b-e5b249f67c67", "type": "expense", "name": "concerts, shows etc", "balance": 107.25, "children": [ ] }] }, { "id": "acc.1260446672222.24", "type": "admin", "name": "administrative expenses", "balance": 0, "children": [ ] }, { "id": "acc.1260446672238.26", "type": "travel", "name": "travel , subsistence account", "balance": -14.5, "children": [ ] }, { "id": "acc.1260446672238.28", "type": "legal", "name": "legal , professional costs account", "balance": 0, "children": [ ] }, { "id": "acc.1260446672238.36", "type": "other_expenses", "name": "rent/rates", "balance": 0, "children": [ ] }, { "id": "acc.1262191376548.37", "type": "expense", "name": "research", "balance": 0, "children": [ ] }, { "id": "acc.1262191388329.38", "type": "expense", "name": "professional development", "balance": 0, "children": [ ] }, { "id": "acc.1262192291558.52", "type": "expense", "name": "professional presentation", "balance": 0, "children": [ ] }, { "id": "acc.1262193596634.72", "type": "expense", "name": "subscriptions", "balance": 0, "children": [ ] }, { "id": "acc.1262265941130.16", "type": "expense", "name": "piano accompaniment", "balance": 0, "children": [ ] }, { "id": "acc.1267370824329.1", "type": "expense", "name": "cost of sales", "balance": 0, "children": [ ] }] }]
what need flatten array have flat list of accounts. what's way proceed in vanilla javascript. (i got access lodash
methods in project).
just iterate on , if children found array children concatinated.
function flat(array) { var result = []; array.foreach(function (a) { result.push(a); if (array.isarray(a.children)) { result = result.concat(flat(a.children)); } }); return result; } var data = [{ id: "acc.1260446672222.11", type: "expenses_folder", name: "expense group", balance: 3418.11, children: [{ id: "acc.1260446672238.27", type: "expense", name: "advertising, promotion , entertainment account", balance: 0, children: [] }, { id: "acc.9a2492ba-0d82-4f4a-a1b4-14868f1e1a39", type: "expenses_folder", name: "premises costs", balance: 0, children: [{ id: "acc.287ba5b6-5536-428b-950f-d71d2af73ccc", type: "expense", name: "use of home - gas", balance: 0, children: [] }, { id: "acc.7091ee15-3f02-4bd1-94e5-5918cf986969", type: "expense", name: "hire of venue, studios, teaching rooms", balance: 0, children: [] }] }, { id: "acc.827ec446-edeb-4f2b-8032-d306292d2d83", type: "expenses_folder", name: "administrative expenses", balance: 558.61, children: [{ id: "acc.0ed5fc81-7734-4452-86a9-db22a6b0f8e8", type: "expense", name: "bank charges", balance: 15, children: [] }, { id: "acc.e2cdb2c0-8565-4991-a35a-d4596b0ddf45", type: "expense", name: "software & computer peripherals", balance: 417.13, children: [] }, { id: "acc.96d5d00e-43f4-4d3a-b97b-fdf258c65514", type: "expense", name: "printing, photocopying etc", balance: 55.93, children: [] }, { id: "acc.494dd64a-4fb3-42b8-be3e-8f3b59a2ef59", type: "expense", name: "artists administration service", balance: 0, children: [] }, { id: "acc.1260446672238.35", type: "expense", name: "stationery", balance: 0, children: [] }, { id: "acc.96d89d0d-5465-488b-b37f-d41ca114c5e6", type: "expense", name: "mobile telephone", balance: 41.19, children: [] }, { id: "acc.1260446672238.33", type: "expense", name: "home telephone", balance: 0, children: [] }, { id: "acc.1260446672238.38", type: "expense", name: "postage/delivery", balance: 29.36, children: [] }] }, { id: "acc.b9c9bbc7-43df-472e-9ac8-c7c76f08f49a", type: "expenses_folder", name: "instruments, equipment maintenance etc", balance: 1002.48, children: [{ id: "acc.1260446672238.32", type: "other_expenses", name: "instrument insurance", balance: 157.48, children: [] }, { id: "acc.2a1cca15-2868-4770-a3e7-d43a6268c6a1", type: "expense", name: "instrument repairs & maintenance", balance: 845, children: [] }, { id: "acc.a908aee0-84fb-450a-916b-4cec25265aef", type: "expense", name: "accessories & replacement parts", balance: 0, children: [] }] }, { id: "acc.a42cdd86-0d9e-4f3f-af0d-7c4525374731", type: "expenses_folder", name: "motor vehicle", balance: 0, children: [{ id: "acc.cb325e7e-0ce4-4c78-9cb4-20659df733a6", type: "expense", name: "fuel , oil", balance: 0, children: [] }] }, { id: "acc.4bdd9e26-ce64-4e7f-b46a-82ec9de06ded", type: "expenses_folder", name: "other travel", balance: 132.1, children: [{ id: "acc.77dd2142-f2de-4a2c-9247-061d0661bc0a", type: "expense", name: "taxis", balance: 24.5, children: [] }, { id: "acc.2b54abdd-7ef5-43cd-bdb9-c8c981b59ff2", type: "expense", name: "public transport", balance: 107.6, children: [] }] }, { id: "acc.e4695b70-31fa-4e23-afd0-97335dcd5b9e", type: "expense", name: "subsitence", balance: 0, children: [] }, { id: "acc.02d222bf-4dff-4308-afe9-69b93f412ada", type: "expense", name: "hotel , accomodation", balance: 0, children: [] }, { id: "acc.d61cd5b4-2c80-4ab8-93d0-9d5726bd253b", type: "expenses_folder", name: "fees , commission paid", balance: 0, children: [{ id: "acc.1262189019758.7", type: "expense", name: "pupils exam entry fees", balance: 0, children: [] }, { id: "acc.a7d7efd3-d0da-4704-babb-079b6077f3fe", type: "expense", name: "audition, competition entry fees", balance: 0, children: [] }, { id: "acc.3b91ee4e-40a8-46d8-aa05-3afa5974b3ef", type: "expense", name: "deputies, other musicians", balance: 0, children: [] }] }, { id: "acc.250d6872-6023-4599-a0b6-b7159eebbfa1", type: "expenses_folder", name: "other professional expenses", balance: 1739.42, children: [{ id: "acc.b7315228-f85a-4ffb-9199-d1128a409e5f", type: "expense", name: "promotion & publicity", balance: 138.6, children: [] }, { id: "acc.69ca2005-d7a0-448b-b70c-dafb128a48ae", type: "expense", name: "other expenses", balance: 364.5, children: [] }, { id: "acc.dcd999d2-4e18-41be-b9cc-218d4034b88e", type: "expense", name: "office equipment, furniture", balance: 0, children: [] }, { id: "acc.e0460706-d5c9-4c40-9d1e-0d2058864b92", type: "expense", name: "cds, dowloads etc", balance: 67.57, children: [] }, { id: "acc.1866df79-9e44-459a-a978-727904987469", type: "expense", name: "professional books, magazines", balance: 104.01, children: [] }, { id: "acc.24c1651d-e7ae-48bc-a32d-311427e0fcea", type: "expense", name: "professional associations", balance: 272.17, children: [] }, { id: "acc.289ab0ac-b9d3-435e-ac82-9da9702b7d4b", type: "expense", name: "tuition", balance: 470, children: [] }, { id: "acc.f24cf99b-6291-4b9f-821e-425f4909d4e1", type: "expense", name: "scores, manuscript paper etc", balance: 215.32, children: [] }, { id: "acc.1af95953-56f0-455e-9d0a-7c4e0477cf0d", type: "expense", name: "performance clothing", balance: 0, children: [] }, { id: "acc.c0585577-535a-4ae2-a02b-e5b249f67c67", type: "expense", name: "concerts, shows etc", balance: 107.25, children: [] }] }, { id: "acc.1260446672222.24", type: "admin", name: "administrative expenses", balance: 0, children: [] }, { id: "acc.1260446672238.26", type: "travel", name: "travel , subsistence account", balance: -14.5, children: [] }, { id: "acc.1260446672238.28", type: "legal", name: "legal , professional costs account", balance: 0, children: [] }, { id: "acc.1260446672238.36", type: "other_expenses", name: "rent/rates", balance: 0, children: [] }, { id: "acc.1262191376548.37", type: "expense", name: "research", balance: 0, children: [] }, { id: "acc.1262191388329.38", type: "expense", name: "professional development", balance: 0, children: [] }, { id: "acc.1262192291558.52", type: "expense", name: "professional presentation", balance: 0, children: [] }, { id: "acc.1262193596634.72", type: "expense", name: "subscriptions", balance: 0, children: [] }, { id: "acc.1262265941130.16", type: "expense", name: "piano accompaniment", balance: 0, children: [] }, { id: "acc.1267370824329.1", type: "expense", name: "cost of sales", balance: 0, children: [] }] }], result = flat(data); document.write('<pre>' + json.stringify(result, 0, 4) + '</pre>');
Comments
Post a Comment