合并对象数组中的重复多维对象

时间:2017-10-03 17:23:07

标签: javascript jquery arrays merge

我希望找到一种基于id值组合数组中对象的方法。我可以使用filter method作为顶层,但不确定如何确保所有子组等都正确合并。这是一个例子:

[
    {
        id: 1,
        name: "A"
        subGroup: [
            {
                id: 1,
                name: "AA"
            }
        ]
    },
    {
        id: 2,
        name: "B"
        subGroup: [
            {
                id: 4,
                name: "DD"
            }
        ]
    },
    {
        id: 1,
        name: "A"
        subGroup: [
            {
                id: 2,
                name: "BB"
            }
        ]
    },
    {
        id: 3,
        name: "C"
        subGroup: [
            {
                id: 5,
                name: "EE"
            }
        ]
    },
    {
        id: 2,
        name: "B"
        subGroup: [
            {
                id: 4,
                other: "Something else"
            }
        ]
    },
    {
        id: 1,
        name: "A"
        subGroup2: [
            {
                id: 7,
                name: "GG"
            }
        ]
    },
    {
        id: 1,
        name: "A"
        subGroup: [
            {
                id: 1,
                name: "AA",
                subSubGroup: [
                    id: 10,
                    name: "II",
                ]
            }
        ]
    }
]

我希望采取类似的方式并将其合并为:

[
    {
        id: 1,
        name: "A"
        subGroup: [
            {
                id: 1,
                name: "AA",
                subSubGroup: [
                    id: 10,
                    name: "II",
                ]
            },
            {
                id: 2,
                name: "BB"
            }
        ],
        subGroup2: [
            {
                id: 7,
                name: "GG"
            }
        ]
    },
    {
        id: 2,
        name: "B"
        subGroup: [
            {
                id: 4,
                name: "DD",
                other: "Something else"
            }
        ]
    },
    {
        id: 3,
        name: "C"
        subGroup: [
            {
                id: 5,
                name: "EE"
            }
        ]
    }
]

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以对idname属性上的数组和组属性使用递归方法。

var data = [{ id: 1, name: "A", subGroup: [{ id: 1, name: "AA" }] }, { id: 2, name: "B", subGroup: [{ id: 4, name: "DD" }] }, { id: 1, name: "A", subGroup: [{ id: 2, name: "BB" }] }, { id: 3, name: "C", subGroup: [{ id: 5, name: "EE" }] }, { id: 2, name: "B", subGroup: [{ id: 4, other: "Something else" }] }, { id: 1, name: "A", subGroup2: [{ id: 7, name: "GG" }] }, { id: 1, name: "A", subGroup: [{ id: 1, name: "AA", subSubGroup: [{ id: 10, name: "II" }] }] }],
    result = [];

data.forEach(function iter(r) {
    return function (o) {
        var ref = r.find(p => o.id === p.id && o.name === p.name);
        if (!ref) {
            r.push(o);
            return;
        }
        Object
            .keys(o)
            .filter(k => Array.isArray(o[k]))
            .forEach(k => o[k].forEach(iter(ref[k] = ref[k] || [])));
    };
}(result));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }