如何基于相同的值合并2个json?

时间:2020-04-21 13:13:35

标签: javascript json angular typescript tree

我正在使用Angular Material Tree控件,并希望在设置它之前合并2个json。我想基于m_id合并它们,删除重复项,还基于sort_id值对节点内的对象进行排序。我尝试查看其他线程中提出的解决方案,但没有发现它们适用于深层结构。任何帮助/指导将不胜感激。

例如:

JSON1:

var json1 = {
data: [{
        name: "Node 1",
        m_id: "node1",
        sort_id: 9,
        children: [{
                name: "Node 1_1",
                m_id: "node1_1",
                sort_id: 6,
                children: [{
                        name: "Node 1_1_1",
                        m_id: "node1_1_1",
                        sort_id: 9,
                        children: []
                    },
                    {
                        name: "Node 1_1_2",
                        m_id: "node1_1_2",
                        sort_id: 2,
                        children: []
                    }
                ]
            },
            {
                name: "Node 1_2",
                m_id: "node1_2",
                sort_id: 3,
                children: [{
                        name: "Node 1_2_1",
                        m_id: "node1_2_1",
                        sort_id: 1,
                        children: []
                    },
                    {
                        name: "Node 1_2_2",
                        m_id: "node1_2_2",
                        sort_id: 5,
                        children: []
                    }
                ]
            }
        ]
    },
    {
        name: "Node 2",
        m_id: "node2",
        sort_id: 6,
        children: [{
                name: "Node 2_1",
                m_id: "node2_1",
                sort_id: 3,
                children: []
            },
            {
                name: "Node 2_2",
                m_id: "node2_2",
                sort_id: 2,
                children: []
            }
        ]
    }
]};

JSON2:

var json2 = {
    data: [{
            name: "Node 1",
            m_id: "node1",
            sort_id: 9,
            children: [{
                    name: "Node 1_1",
                    m_id: "node1_1",
                    sort_id: 6,
                    children: [{
                            name: "Node 1_1_1",
                            m_id: "node1_1_1",
                            sort_id: 9,
                            children: []
                        },
                        {
                            name: "Node 1_1_3",
                            m_id: "node1_1_3",
                            sort_id: 5,
                            children: []
                        }
                    ]
                },
                {
                    name: "Node 1_2",
                    m_id: "node1_2",
                    sort_id: 3,
                    children: [{
                            name: "Node 1_2_1",
                            m_id: "node1_2_1",
                            sort_id: 1,
                            children: []
                        },
                        {
                            name: "Node 1_2_2",
                            m_id: "node1_2_2",
                            sort_id: 5,
                            children: []
                        },
                        {
                            name: "Node 1_2_3",
                            m_id: "node1_2_3",
                            sort_id: 7,
                            children: []
                        }
                    ]
                }
            ]
        },
        {
            name: "Node 4",
            m_id: "node4",
            sort_id: 8,
            children: [{
                    name: "Node 4_1",
                    m_id: "node4_1",
                    sort_id: 4,
                    children: []
                },
                {
                    name: "Node 4_2",
                    m_id: "node4_2",
                    sort_id: 1,
                    children: []
                }
            ]
        }
    ]
};

1 个答案:

答案 0 :(得分:0)

可能使用lodash之类的库。

只需解析json,然后使用https://lodash.com/docs/#merge