javascript - 将 json 对象排序到嵌套树中

标签 javascript jquery sorting tree

我正在从 Web 服务中提取两个相关对象 - 文件夹和电子邮件。文件夹有一个 ID 和一个 parentfolder.ID 属性,指示文件夹嵌套在哪个父文件夹下。电子邮件有一个 CategoryID,表明它是哪个文件夹的子文件夹。

我已经成功地创建了一个将电子邮件嵌套在平面文件夹结构中的函数:

{
"folders": [
    {
        "name": "my emails",
        "type": "folder",
        "additionalParameters": {
            "id": "174661",
            "type": "email",
            "parentID": "0"
        },
        "children": [
            {
                "name": "Test1",
                "type": "item",
                "additionalParameters": {
                    "id": "27502",
                    "subject": "Test"
                }
            },
            {
                "name": "Hello",
                "type": "item",
                "additionalParameters": {
                    "id": "27917",
                    "subject": "Hi!"
                }
            }
        ]
    },
    {
        "name": "Test",
        "type": "folder",
        "additionalParameters": {
            "id": "175620",
            "type": "email",
            "parentID": "174661"
        },
        "children": [
            {
                "name": "Test2",
                "type": "item",
                "additionalParameters": {
                    "id": "27891",
                    "subject": "Test"
                }
            }
        ]
    },
    {
        "name": "SubFolder1",
        "type": "folder",
        "additionalParameters": {
            "id": "175621",
            "type": "email",
            "parentID": "175620"
        },
        "children": [
            {
                "name": "Test2",
                "type": "item",
                "additionalParameters": {
                    "id": "27892",
                    "subject": "Test"
                }
            },
            {
                "name": "Test3",
                "type": "item",
                "additionalParameters": {
                    "id": "27893",
                    "subject": "Test"
                }
            }
        ]
    },
    {
        "name": "SubFolder2",
        "type": "folder",
        "additionalParameters": {
            "id": "175622",
            "type": "email",
            "parentID": "175620"
        },
        "children": [
            {
                "name": "Test4",
                "type": "item",
                "additionalParameters": {
                    "id": "27894",
                    "subject": "Test"
                }
            }
        ]
    }
]
}

现在我需要使用递归循环遍历所有文件夹并将它们插入其父级的 children 数组中。本质上是将树求助于 n 个级别。我可以忽略任何 type=items,因为它们已经正确嵌套。只需要对类型为文件夹的进行排序即可。

有没有人实现过JSON递归函数,通过嵌套重建JSON对象?

感谢您的帮助。

最佳答案

您无需递归即可完成此操作。我回答了similar question有时回来。我相信您可以使用相同的方法(假设您没有前向引用):

var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup
var root = null; //Initially set our root to null

//loop over data
for(var i = 0; i < data.folders.length; i++) {
    var folder = data.folders[i];

    //each node will have children, so let's give it a "children" poperty
    folder.children = [];

    //add an entry for this node to the map so that any future children can
    //lookup the parent
    idToNodeMap[folder.additionalParameters.id] = folder;

    //Does this node have a parent?
    if(folder.additionalParamters.parentID === "0") {
        //Doesn't look like it, so this node is the root of the tree
        root = folder;        
    } else {        
        //This node has a parent, so let's look it up using the id
        parentNode = idToNodeMap[folder.additionalParamters.parentID];    

        //Let's add the current node as a child of the parent node.
        parentNode.children.push(folder);        
    }
}

关于javascript - 将 json 对象排序到嵌套树中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17554768/

相关文章:

javascript - 如果 ul 存在则 addClass (jQuery)

javascript - 无法在 React.js 上从 svg 加载图像

javascript - 布局/masonry - 延迟布局调整 - 一键落后

jQuery 内容 Controller 没有正确显示和隐藏内容

javascript - textarea 根据屏幕大小改变字体大小

javascript - Cypress:如何模拟键盘(CTRL/SHIFT)操作

javascript - 是否可以在 Javascript 的 css3 转换期间获取 objective-c ss 属性值?

c - 我对数组进行排序的代码有问题吗

angularjs - ngTable 不排序?

jquery - 对 li 项内的 anchor 进行排序,而不对嵌套列表进行排序