遍历递归json树并合并

时间:2016-04-30 13:48:46

标签: javascript jquery json recursion

我试图遍历递归json树如下(json2)并与另一个json(json)合并,如果标识符匹配如下。请注意,每当对象可用时,它可能包含对象对象,但标识符将在同一对象层次结构中。

注意:标识符值始终是唯一的

我已经遍历了underscore.js库,但无法找到符合此要求的任何函数。在正常的列表情况下,findWhere就足以解决了,但事实并非如此,因为它具有多级层次结构。

JSON 2:

{
    "identifier1": "123456",
    "identifier2": "234567"
}

JSON 1:

{
    subtopic: [
        {
            "title": "title 1",
            "subtitle": "title 2",
            "objects": [{
                "title": "123"
                "label": "456"
                "objects": [
                    {
                        "identifier": "identifier1",
                        "object": {
                            "object-1": "123",
                            "object-2": "456"
                        }
                    },
                    {
                        "identifier": "identifier2",
                        "object": {
                            "object-1": "123",
                            "object-2": "456"
                        }
                    }
                ]
            }]
        },
        {
            ...
            ...
            Similar as above
        },
    ]
}

合并后

{
    subtopic: [
        {
            "title": "title 1",
            "subtitle": "title 2",
            "objects": [{
                "title": "123"
                "label": "456"
                "objects": [
                    {
                        "result": "123456",
                        "identifier": "identifier1",
                        "object": {
                            "object-1": "123",
                            "object-2": "456"
                        }
                    },
                    {
                        "result": "234567"
                        "identifier": "identifier2",
                        "object": {
                            "object-1": "123",
                            "object-2": "456"
                        }
                    }
                ]
            }]
        },
        {
            ...
            ...
            Similar as above
        },
    ]
}

1 个答案:

答案 0 :(得分:1)

至少你需要一些迭代和递归样式来获取正确的内部对象来设置新属性。



function merge(array, object) {
    Object.keys(object).forEach(function (k) {
        function iter(a) {
            if (a.identifier === k) {
                r = a;
                return true;
            }
            return Array.isArray(a.objects) && a.objects.some(iter);
        }

        var r;
        array.some(iter);                            
        if (r) {
            r.result = object[k];
        }
    });
}

var object2 = { "identifier1": "123456", "identifier2": "234567" },
    object1 = { subtopic: [{ "title": "title 1", "subtitle": "title 2", "objects": [{ "title": "123", "label": "456", "objects": [{ "identifier": "identifier1", "object": { "object-1": "123", "object-2": "456" } }, { "identifier": "identifier2", "object": { "object-1": "123", "object-2": "456" } }] }] }] };

merge(object1.subtopic, object2);
document.write('<pre>' + JSON.stringify(object1, 0, 4) + '</pre>');
&#13;
&#13;
&#13;