简体   繁体   English

我有一个扁平数组作为输入,我需要以树格式重建它

[英]I have a flattened array as input, I need to reconstruct it in a tree format

Also the one node with null as parent_uid is the top-most node.此外,以null作为parent_uid的一个节点是最顶层的节点。 Input -:输入 -:

[
{uid: "d86161ab-1838-4cd3-afc2-20a32c08e88f", parent_uid: null},
{uid: "c64eb64e-1291-4833-b646-947f1a64a1cf", parent_uid: "d86161ab-1838-4cd3-afc2-20a32c08e88f"},
{uid: "93976670-6272-4cca-ac18-73bb3345d95c", parent_uid: "d86161ab-1838-4cd3-afc2-20a32c08e88f"},
{uid: "17c19db6-cde9-4581-a49e-c279faae922c", parent_uid: "d86161ab-1838-4cd3-afc2-20a32c08e88f"},
{uid: "ebb92286-58e5-49dc-a9db-30a5aec378b0", parent_uid: "d86161ab-1838-4cd3-afc2-20a32c08e88f"},
{uid: "df25c86a-b34a-42f4-8c4c-7c419dc97dc8", parent_uid: "df25c86a-b34a-42f4-8c4c-7c419dc97dc8"},
{uid: "f3bbe5bf-d56b-4e00-9991-40f27cf2b8e8", parent_uid: "df25c86a-b34a-42f4-8c4c-7c419dc97dc8"},
{uid: "9de2cd38-ba16-4ec0-bcd7-b2d1a4a75e3f", parent_uid: "df25c86a-b34a-42f4-8c4c-7c419dc97dc8"},
{uid: "f800ed46-a894-418e-bb3a-e0c317a244fa", parent_uid: "d86161ab-1838-4cd3-afc2-20a32c08e88f"},
{uid: "11eb90e1-b527-4e88-be70-cad4a2a60bdd", parent_uid: "d86161ab-1838-4cd3-afc2-20a32c08e88f"},
{uid: "aaa6ff31-13d2-57d0-ef49-d8962884cedb", parent_uid: "d86161ab-1838-4cd3-afc2-20a32c08e88f"},
{uid: "81004b95-37e8-9096-918c-64fbab3b7cd7", parent_uid: "d86161ab-1838-4cd3-afc2-20a32c08e88f"},
{uid: "1cfdacf8-c7bb-481c-503a-ef88665a07c9", parent_uid: "d86161ab-1838-4cd3-afc2-20a32c08e88f"},
{uid: "13e4e12a-2e33-f69f-fe83-5fb9c2d7a869", parent_uid: "9de2cd38-ba16-4ec0-bcd7-b2d1a4a75e3f"},
{uid: "c480d612-0a67-105f-efc1-dd248962f1fd", parent_uid: "9de2cd38-ba16-4ec0-bcd7-b2d1a4a75e3f"},
{uid: "44a7b3c8-5672-fac1-1b98-1f173cab2737", parent_uid: "13e4e12a-2e33-f69f-fe83-5fb9c2d7a869"}
]

You could do this with recursive approach using reduce method and pass down the uid and then check it with parent_uid in nested calls.您可以使用reduce方法通过递归方法执行此操作并传递uid ,然后在嵌套调用中使用parent_uid检查它。

 const data = [{"uid":"d86161ab-1838-4cd3-afc2-20a32c08e88f","parent_uid":null},{"uid":"c64eb64e-1291-4833-b646-947f1a64a1cf","parent_uid":"d86161ab-1838-4cd3-afc2-20a32c08e88f"},{"uid":"93976670-6272-4cca-ac18-73bb3345d95c","parent_uid":"d86161ab-1838-4cd3-afc2-20a32c08e88f"},{"uid":"17c19db6-cde9-4581-a49e-c279faae922c","parent_uid":"d86161ab-1838-4cd3-afc2-20a32c08e88f"},{"uid":"ebb92286-58e5-49dc-a9db-30a5aec378b0","parent_uid":"d86161ab-1838-4cd3-afc2-20a32c08e88f"},{"uid":"df25c86a-b34a-42f4-8c4c-7c419dc97dc8","parent_uid":"df25c86a-b34a-42f4-8c4c-7c419dc97dc8"},{"uid":"f3bbe5bf-d56b-4e00-9991-40f27cf2b8e8","parent_uid":"df25c86a-b34a-42f4-8c4c-7c419dc97dc8"},{"uid":"9de2cd38-ba16-4ec0-bcd7-b2d1a4a75e3f","parent_uid":"df25c86a-b34a-42f4-8c4c-7c419dc97dc8"},{"uid":"f800ed46-a894-418e-bb3a-e0c317a244fa","parent_uid":"d86161ab-1838-4cd3-afc2-20a32c08e88f"},{"uid":"11eb90e1-b527-4e88-be70-cad4a2a60bdd","parent_uid":"d86161ab-1838-4cd3-afc2-20a32c08e88f"},{"uid":"aaa6ff31-13d2-57d0-ef49-d8962884cedb","parent_uid":"d86161ab-1838-4cd3-afc2-20a32c08e88f"},{"uid":"81004b95-37e8-9096-918c-64fbab3b7cd7","parent_uid":"d86161ab-1838-4cd3-afc2-20a32c08e88f"},{"uid":"1cfdacf8-c7bb-481c-503a-ef88665a07c9","parent_uid":"d86161ab-1838-4cd3-afc2-20a32c08e88f"},{"uid":"13e4e12a-2e33-f69f-fe83-5fb9c2d7a869","parent_uid":"9de2cd38-ba16-4ec0-bcd7-b2d1a4a75e3f"},{"uid":"c480d612-0a67-105f-efc1-dd248962f1fd","parent_uid":"9de2cd38-ba16-4ec0-bcd7-b2d1a4a75e3f"},{"uid":"44a7b3c8-5672-fac1-1b98-1f173cab2737","parent_uid":"13e4e12a-2e33-f69f-fe83-5fb9c2d7a869"}] function toTree(data, pid = null) { return data.reduce((r, e) => { if (pid === e.parent_uid) { const obj = {...e } const children = toTree(data, e.uid); if (children.length) obj.children = children; r.push(obj) } return r; }, []) } const result = toTree(data); console.log(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有一个数组,我需要使用 javascript 更改数组格式 - i have an array and i need to change an array format using javascript 处理来自ElasticSearch的对象-我需要重建它吗? - Dealing with Objects from ElasticSearch - Do I need to reconstruct it? 我需要以这种格式插入 json "[{\"id\":1,\"name\":\"Eat Fresh\",\"app_type\":1,\"price\":\"500\ "}]" 但我有一个这种格式的数组 - i need to insert json in this format "[{\"id\":1,\"name\":\"Eat Fresh\",\"app_type\":1,\"price\":\"500\"}]" but i have a array in this format 如何从 forEach 循环中实现扁平化的 promise 数组? - How can I achieve a flattened array of promises from a forEach loop? 如何从阵列数组创建“扁平”组? - How can I create a “flattened” group from an array of arrays? js / reactjs-如何将json中的数据重构为数组? - js/reactjs - How do i reconstruct data from a json into a array? 我无权访问数组,但需要删除逗号 - I have no access to array, but need to remove commas 我有一个数组,我需要使用 object 映射将其推入新数组 - I have an Array I need to push into new Array with object mapping 我有一个需要在 object (JAVASCRIPT) 中转换的数组 - I have an array that I need to transform in a object (JAVASCRIPT) 我有一个数组,我只需要提取数字 - I have an array which I need to extract only number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM