【问题标题】:How to convert data structure to another tree structure如何将数据结构转换为另一种树结构
【发布时间】:2021-02-24 11:44:33
【问题描述】:

我需要您的帮助来创建具有给定 JSON 的树状数据结构,如下所示,所需的输出结构已经在下面给出。我是编程初学者,对数据结构知之甚少

来自 API 的 JSON

   [
    "",
    [
        "Test",
        [
            "Test/sample",
            [
                "Test/sample/sample1"
            ]
        ],
        [
            "Test/test3"
        ]
    ],
    [
        "Test1",
        [
            "Test1/test2"
        ]
    ],
    [
        "cat"
    ]
]

输出如下结构

 {
        "key": "Image",
        "label": "Image",
        "icon": 'pi pi-folder',
        "children": []
    },

    {
        "key": "Test",
        "label": "Test",
        "icon": "pi pi-folder",

        "children": [
            {
                "key": "Test/sample",
                "label": "Sample",
                "icon": "pi pi-folder",

                "children": [{
                    "key": "Test/sample/sample1",
                    "label": "Sample1",
                    "icon": 'pi pi-folder',
                    "children": []
                }]
            },
            {
                "key": "Test/test3",
                "label": "test3",
                "icon": "pi pi-folder",
                "children": []
            },
        ]
    },

    {
        "key": "Test1",
        "label": "Test1",
        "icon": "pi pi-folder",
        "children": [{
            "key": "Test1/test2",
            "label": "test2",
            "icon": "pi pi-folder",
            "children": []
        }]
    },

    {
        "key": "cat",
        "label": "cat",
        "icon": "pi pi-folder",
        "children": []
    },

]

以上结构为图片文件目录,目录名用标签表示,图标表示目录,子目录显示子目录

Console value of API

【问题讨论】:

  • 如果可能的话,您能否在第一个 JSON 数据上添加更多信息(即,每个数组似乎代表其子级的前缀,由 '/' 分隔,但这是猜测)。另外,到目前为止,您是否有任何现有的研究/代码可以帮助您指明正确的方向?
  • 您能否为您的问题提供完整的 json 输入?
  • 哦,我没有看到键/值 Image 所以我假设您的 API 中的 JSON 缺少一些值
  • 我的回答有用吗?
  • 您可以先阅读有关递归函数的文章,例如 this one。如果你想改变结构,只需编辑createChildNode函数

标签: javascript arrays multidimensional-array data-structures tree


【解决方案1】:

使用递归函数可以轻松存档

const icon = "pi pi-folder";
var a = [
    "",
    [
        "Test",
        [
            "Test/sample",
            [
                "Test/sample/sample1"
            ]
        ],
        [
            "Test/test3"
        ]
    ],
    [
        "Test1",
        [
            "Test1/test2"
        ]
    ],
    [
        "cat"
    ]
]

/**
 * @param {Array} arr The child array
 */
function createChildNode(arr) {
    let key = "Image";
    let label = "Image";
    let children = [];
    if (arr.length >= 1) {
        key = arr[0];
        label = key.includes("/") ? key.split("/").pop() : key;
        label = label.charAt(0).toUpperCase() + label.slice(1);
        for (let index = 1; index < arr.length; index++) {
            const element = arr[index];
            children.push(createChildNode(element));
        }
    }
    return {key: key, label: label, icon: icon, children: children};
}

var b = []
for (let index = 0; index < a.length; index++) {
    const element = a[index];
    b.push(createChildNode(element));
}
console.log(b);

【讨论】:

    猜你喜欢
    • 2019-08-27
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多