【问题标题】:Removing Duplicates from array of object and merging it's Subarray into one从对象数组中删除重复项并将其子数组合并为一个
【发布时间】:2020-04-28 01:42:00
【问题描述】:

我有一个对象数组,其中有一个数据索引作为子数组。对象数组返回重复值。所以我必须对其进行分组,使其包含所有关键字段,所有数据都合并在该数组的数据索引。

这是我得到的示例代码:

"Todo": [
        {
            "AreaId": 4,
            "AreaName": "Hall",
            "Sequence": 3,
            "Data": [
                {
                    "AssetId": 2,
                    Some OtherFields,

                }
            ]
        },
        {
            "AreaId": 4,
            "AreaName": "Hall",
            "Sequence": 3,
            "Data": [
                {
                    "AssetId": 3,
                     Some OtherFields,
                }
            ]
        },


        {
                "AreaId": 2,
                "AreaName": "Hall",
                "Sequence": 1,
                "Data": [
                    {
                        "AssetId": 4,
                        Some OtherFields,

                    }
                ]
            },
            {
                "AreaId": 2,
                "AreaName": "Hall",
                "Sequence": 1,
                "Data": [
                    {
                        "AssetId": 3,
                         Some OtherFields,
                    }
                ]
            }

]

我希望这个输出如下所述:

 "Todo": [
            {
                "AreaId": 4,
                "AreaName": "Hall",
                "Sequence": 3,
                "Data": [
                    {
                        "AssetId": 2,
                         Some OtherFields,
                    },
                    {
                        "AssetId": 3,
                        Some OtherFields,
                    }

                ]
            },




       {
            "AreaId": 2,
            "AreaName": "Hall",
            "Sequence": 3,
            "Data": [
                {
                    "AssetId": 4,
                     Some OtherFields,
                },
                {
                    "AssetId": 3,
                    Some OtherFields,
                }

            ]
        }
]

【问题讨论】:

    标签: node.js merge duplicates grouping


    【解决方案1】:

    您可以使用reduce 合并对象:

    我们在这里按 AreaId、AreaName 和 Sequence 进行分组,更准确地说,我们使用除 Data 之外的所有其他属性来对对象进行分组。

    例如:

    const data = { "Todo": [ { "AreaId": 4, "AreaName": "Hall", "Sequence": 3, "Data": [ { "AssetId": 2, "SomeOtherField1": "Value1", "SomeOtherField2": "Value2" } ] }, { "AreaId": 4, "AreaName": "Hall", "Sequence": 3, "Data": [ { "AssetId": 3, "SomeOtherField1": "Value1", "SomeOtherField2": "Value2" } ] }, { "AreaId": 2, "AreaName": "Hall", "Sequence": 1, "Data": [ { "AssetId": 4, "SomeOtherField1": "Value1", "SomeOtherField2": "Value2" } ] }, { "AreaId": 2, "AreaName": "Hall", "Sequence": 1, "Data": [ { "AssetId": 3, "SomeOtherField1": "Value1", "SomeOtherField2": "Value2" } ] } ] }; 
    
    // Let's do a reduce here..
    const result = { Todo: Object.values(data.Todo.reduce((acc, elem ) => {
        // Create a key based on AreaId
        const key = elem.AreaId;
        if (!acc[key]) { 
            acc[key] = { ...elem };
            acc[key].Data = [];
        }
        acc[key].Data = [...(acc[key].Data || []), elem.Data];
        return acc;
    }, {}))};
    
    console.log("Result:", result);

    【讨论】:

    • 实际上这是在其他数组中。它是嵌套数组。首先有一个输出数组,下面有一个索引 Todo ,它还包含一个索引 Data ,我想根据 AreaId 属性合并这个 Data 索引。
    • 应该是很相似的代码..我们只是将key切换到AreaId。如果 ToDo 是一个数组条目而不是一个对象属性,这也不应该对代码进行太多更改!
    猜你喜欢
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 2018-09-11
    • 2022-01-24
    相关资源
    最近更新 更多