小编典典

带有父ID的单元的分层json

json

http://jsfiddle.net/eYgGK/

我从另一个帖子中偷走了这个脚本:

 function convertToHierarchy() {
var arry = [{ "Id": "1", "Name": "abc", "Parent": "", "attr": "abc" },
               { "Id": "2", "Name": "abc", "Parent": "1", "attr": "abc" },
               { "Id": "3", "Name": "abc", "Parent": "2", "attr": "abc" },
               { "Id": "4", "Name": "abc", "Parent": "2", "attr": "abc" }];
        var nodeObjects = createStructure(arry);
        for (var i = nodeObjects.length - 1; i >= 0; i--) {
            var currentNode = nodeObjects[i];
            if (currentNode.value.Parent === "") {
                continue;
            }
            var parent = getParent(currentNode, nodeObjects);

            if (parent === null) {
                continue;
            }

            parent.children.push(currentNode);
            nodeObjects.splice(i, 1);
        }
        console.dir(nodeObjects);
        return nodeObjects;
    }
    function createStructure(nodes) {
        var objects = [];

        for (var i = 0; i < nodes.length; i++) {
            objects.push({ value: nodes[i], children: [] });
        }

        return objects;

    }
    function getParent(child, nodes) {
        var parent = null;

        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].value.Id === child.value.Parent) {
                return nodes[i];
            }
        }

        return parent;
    }

该脚本产生:

[{
    "value": {
        "Id": "1",
        "Name": "abc",
        "Parent": "",
        "attr": "abc"
    },
    "children": [{
        "value": {
            "Id": "2",
            "Name": "abc",
            "Parent": "1",
            "attr": "abc"
        },
        "children": [{
            "value": {
                "Id": "4",
                "Name": "abc",
                "Parent": "2",
                "attr": "abc"
            },
            "children": []
        }, {
            "value": {
                "Id": "3",
                "Name": "abc",
                "Parent": "2",
                "attr": "abc"
            },
            "children": []
        }]
    }]
}]

我正在寻找的是:

[{
    "Id": "1",
    "Name": "abc",
    "Parent": "",
    "attr": "abc",
    "children": [{

        "Id": "2",
        "Name": "abc",
        "Parent": "1",
        "attr": "abc",
        "children": [{

            "Id": "4",
            "Name": "abc",
            "Parent": "2",
            "attr": "abc"
        }, {

            "Id": "3",
            "Name": "abc",
            "Parent": "2",
            "attr": "abc"

        }]
    }]
}]

我首先需要摆脱“值”包装,其次是空的子节点。我知道我可以编写一个清理脚本,但是那不是最佳实践。如果有人知道如何修复或建议其他脚本,那就太好了!

谢谢


阅读 213

收藏
2020-07-27

共1个答案

小编典典

尝试类似的东西

var arry = [{ "Id": "1", "Name": "abc", "Parent": "", "attr": "abc" },
               { "Id": "2", "Name": "abc", "Parent": "1", "attr": "abc" },
               { "Id": "3", "Name": "abc", "Parent": "2", "attr": "abc" },
               { "Id": "4", "Name": "abc", "Parent": "2", "attr": "abc" }];

function convert(array){
    var map = {};
    for(var i = 0; i < array.length; i++){
        var obj = array[i];
        obj.items= [];

        map[obj.Id] = obj;

        var parent = obj.Parent || '-';
        if(!map[parent]){
            map[parent] = {
                items: []
            };
        }
        map[parent].items.push(obj);
    }

    return map['-'].items;

}

var r = convert(arry)

演示:小提琴

结果

[{
    "Id" : "1",
    "Name" : "abc",
    "Parent" : "",
    "attr" : "abc",
    "children" : [{
                "Id" : "2",
                "Name" : "abc",
                "Parent" : "1",
                "attr" : "abc",
                "children" : [{
                            "Id" : "3",
                            "Name" : "abc",
                            "Parent" : "2",
                            "attr" : "abc",
                            "children" : []
                        }, {
                            "Id" : "4",
                            "Name" : "abc",
                            "Parent" : "2",
                            "attr" : "abc",
                            "children" : []
                        }]
            }]
}]
2020-07-27