在对象数组中合并重复的多维对象 [英] Merge duplicate multidimensional objects in array of objects

查看:119
本文介绍了在对象数组中合并重复的多维对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种基于id值在数组中组合对象的方法.我可以在顶层使用过滤器方法使其工作,但不确定如何确保所有子组等正确合并.这是一个示例:

I am looking to find a way to combine objects in arrays based on id values. I can make it work using the filter method for the top level, but not sure how to make sure that all subGroups and such are merged properly. Here is an example:

[
    {
        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",
                ]
            }
        ]
    }
]

我正在寻找类似的东西并将其合并:

I am looking to take something like that and merge it like:

[
    {
        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"
            }
        ]
    }
]

任何帮助将不胜感激.

推荐答案

对于数组和idname属性上的组,可以使用递归方法.

You could use a recursive approach for properties which are arrays and group on id and name property.

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; }

这篇关于在对象数组中合并重复的多维对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆