在Angular中比较两个不同的对象并移除重复项。

huangapple go评论140阅读模式
英文:

In Angular compare two different objects and remove duplicates

问题

在Angular中,我有这两个包含对象的数组。

  1. 主要对象
[
    {
        "userId": 8,
        "name": "Adfgh",
        "uniqueId": "rthj",
        "source": {
            "key": "0",
            "mode": "d",
            "name": "tev5"
        },
        "headers": [
            {
                "content": "b",
                "id": "01"
            },
            {
                "content": "m",
                "id": "04"
            },
            {
                "content": "R1",
                "id": "02"
            },
            {
                "content": "083083",
                "id": "03"
            },
            {
                "content": "5",
                "id": "05"
            }
        ],
        "key": "IPP Master Key",
        "destination": "5"
    },
    {
        "userId": 8,
        "name": "FGHJKNJKN",
        "uniqueId": "etjhjub",
        "source": {
            "key": "M0",
            "mode": "C",
            "name": "DEVm6"
        },
        "headers": [
            {
                "content": "n",
                "id": "01"
            },
            {
                "content": "m",
                "id": "04"
            },
            {
                "content": "R1",
                "id": "02"
            },
            {
                "content": "083083",
                "id": "03"
            },
            {
                "content": "6",
                "id": "05"
            }
        ],
        "key": "IPP Master Key",
        "destination": "6"
    },
    {
        "userId": 8,
        "name": "A-KEYTST-DEVm7",
        "VRKcustomerId": "VHQ-DUKPT-MSK",
        "sourceKeyObject": {
            "keyUsage": "M0",
            "modeOfUse": "C",
            "name": "DEVm7"
        },
        "headers": [
            {
                "content": "n",
                "id": "01"
            },
            {
                "content": "m",
                "id": "04"
            },
            {
                "content": "R1",
                "id": "02"
            },
            {
                "content": "083083",
                "id": "03"
            },
            {
                "content": "7",
                "id": "05"
            }
        ],
        "keyType": "IPP Master Key",
        "destination": "7"
    },
    {
        "customerId": 8,
        "name": "name",
        "uniqueId": "remove",
        "source": {
            "key": "D0",
            "mode": "B",
            "name": "A-KEYTST-PNP-NHMAC"
        },
        "headers": [
            {
                "content": "1",
                "id": "11"
            },
            {
                "content": "ESDFGH_567",
                "id": "10"
            }
        ],
        "key": null,
        "destination": ""
    },
    {},
    {},
    {},
    {},
    {},
    {},
    {},
]
  1. 第二个对象
[
    {
        "Id": 098,
        "UniqueId": "remove",
        "Destination": "",
        "Key": "",
        "Name": "name",
        "Enable": false,
        "Auto": 0
    },
    {
        "Id": 154,
        "UniqueId": "NEO-TEST",
        "Destination": "",
        "Key": "",
        "Name": "A-KEYTST-PNP-NHMAC",
        "Enable": false,
        "Auto": 0
    },
]

期望结果。

[
    {
        "userId": 8,
        "name": "Adfgh",
        "uniqueId": "rthj",
        "source": {
            "key": "0",
            "mode": "d",
            "name": "tev5"
        },
        "headers": [
            {
                "content": "b",
                "id": "01"
            },
            {
                "content": "m",
                "id": "04"
            },
            {
                "content": "R1",
                "id": "02"
            },
            {
                "content": "083083",
                "id": "03"
            },
            {
                "content": "5",
                "id": "05"
            }
        ],
        "key": "IPP Master Key",
        "destination": "5"
    },
    {
        "userId": 8,
        "name": "FGHJKNJKN",
        "uniqueId": "etjhjub",
        "source": {
            "key": "M0",
            "mode": "C",
            "name": "DEVm6"
        },
        "headers": [
            {
                "content": "n",
                "id": "01"
            },
            {
                "content": "m",
                "id": "04"
            },
            {
                "content": "R1",
                "id": "02"
            },
            {
                "content": "083083",
                "id": "03"
            },
            {
                "content": "6",
                "id": "05"
            }
        ],
        "key": "IPP Master Key",
        "destination": "6"
    },
    {
        "userId": 8,
        "name": "A-KEYTST-DEVm7",
        "VRKcustomerId": "VHQ-DUKPT-MSK",
        "sourceKeyObject": {
            "keyUsage": "M0",
            "modeOfUse": "C",
            "name": "DEVm7"
        },
        "headers": [
            {
                "content": "n",
                "id": "01"
            },
            {
                "content": "m",
                "id": "04"
            },
            {
                "content": "R1",
                "id": "02"
            },
            {
                "content": "083083",
                "id": "03"
            },
            {
                "content": "7",
                "id": "05"
            }
        ],
        "keyType": "IPP Master Key",
        "destination": "7"
    },
]

要比较它们,我创建了一个分离的数组,以使它们相似。我尝试使用过滤器和映射,但没有帮助。

我尝试了不同的方法,但没有成功。所以我没有代码片段。需要根据它们之间的差异比较主要对象和第二个对象,然后修改主要对象。

英文:

In Angular I have this 2 arrays with objects.

  1. Main Object

    [
    {
    "userId": 8,
    "name": "Adfgh",
    "uniqueId": "rthj",
    "source": {
    "key": "0",
    "mode": "d",
    "name": "tev5"
    },
    "headers": [
    {
    "content": "b",
    "id": "01"
    },
    {
    "content": "m",
    "id": "04"
    },
    {
    "content": "R1",
    "id": "02"
    },
    {
    "content": "083083",
    "id": "03"
    },
    {
    "content": "5",
    "id": "05"
    }
    ],
    "key": "IPP Master Key",
    "destination": "5"
    },
    {
    "userId": 8,
    "name": "FGHJKNJKN",
    "uniqueId": "etjhjub",
    "source": {
    "key": "M0",
    "mode": "C",
    "name": "DEVm6"
    },
    "headers": [
    {
    "content": "n",
    "id": "01"
    },
    {
    "content": "m",
    "id": "04"
    },
    {
    "content": "R1",
    "id": "02"
    },
    {
    "content": "083083",
    "id": "03"
    },
    {
    "content": "6",
    "id": "05"
    }
    ],
    "key": "IPP Master Key",
    "destination": "6"
    },
    {
    "userId": 8,
    "name": "A-KEYTST-DEVm7",
    "VRKcustomerId": "VHQ-DUKPT-MSK",
    "sourceKeyObject": {
    "keyUsage": "M0",
    "modeOfUse": "C",
    "name": "DEVm7"
    },
    "headers": [
    {
    "content": "n",
    "id": "01"
    },
    {
    "content": "m",
    "id": "04"
    },
    {
    "content": "R1",
    "id": "02"
    },
    {
    "content": "083083",
    "id": "03"
    },
    {
    "content": "7",
    "id": "05"
    }
    ],
    "keyType": "IPP Master Key",
    "destination": "7"
    },
    {
    "customerId": 8,
    "name": "name",
    "uniqueId": "remove",
    "source": {
    "key": "D0",
    "mode": "B",
    "name": "A-KEYTST-PNP-NHMAC"
    },
    "headers": [
    {
    "content": "1",
    "id": "11"
    },
    {
    "content": "ESDFGH_567",
    "id": "10"
    }
    ],
    "key": null,
    "destination": ""
    },
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    ]

  2. second object

    [
    {
    "Id": 098,
    "UniqueId": "remove",
    "Destination": "",
    "Key": "",
    "Name": "name",
    "Enable": false,
    "Auto": 0
    },
    {
    "Id": 154,
    "UniqueId": "NEO-TEST",
    "Destination": "",
    "Key": "",
    "Name": "A-KEYTST-PNP-NHMAC",
    "Enable": false,
    "Auto": 0
    },
    ]

expected result.

[
    {
        "userId": 8,
        "name": "Adfgh",
        "uniqueId": "rthj",
        "source": {
            "key": "0",
            "mode": "d",
            "name": "tev5"
        },
        "headers": [
            {
                "content": "b",
                "id": "01"
            },
            {
                "content": "m",
                "id": "04"
            },
            {
                "content": "R1",
                "id": "02"
            },
            {
                "content": "083083",
                "id": "03"
            },
            {
                "content": "5",
                "id": "05"
            }
        ],
        "key": "IPP Master Key",
        "destination": "5"
    },
    {
        "userId": 8,
        "name": "FGHJKNJKN",
        "uniqueId": "etjhjub",
        "source": {
            "key": "M0",
            "mode": "C",
            "name": "DEVm6"
        },
        "headers": [
            {
                "content": "n",
                "id": "01"
            },
            {
                "content": "m",
                "id": "04"
            },
            {
                "content": "R1",
                "id": "02"
            },
            {
                "content": "083083",
                "id": "03"
            },
            {
                "content": "6",
                "id": "05"
            }
        ],
        "key": "IPP Master Key",
        "destination": "6"
    },
    {
        "userId": 8,
        "name": "A-KEYTST-DEVm7",
        "VRKcustomerId": "VHQ-DUKPT-MSK",
        "sourceKeyObject": {
            "keyUsage": "M0",
            "modeOfUse": "C",
            "name": "DEVm7"
        },
        "headers": [
            {
                "content": "n",
                "id": "01"
            },
            {
                "content": "m",
                "id": "04"
            },
            {
                "content": "R1",
                "id": "02"
            },
            {
                "content": "083083",
                "id": "03"
            },
            {
                "content": "7",
                "id": "05"
            }
        ],
        "keyType": "IPP Master Key",
        "destination": "7"
    },
]

For to compare them, I created a seperated array for to make them similiar to each other. Used filter, map but it wasn't helpful.

I have tried in different ways, and nothing worked. So I don't have the snippet for the code.
Need to compare main object and second object, based on the difference between them have to modify main object.

答案1

得分: 0

Sure, here's the translated code:

function updateMainObject(mainArray: any[], secondArray: any[]) {
    const updatedMainArray = mainArray.filter((mainItem) => {
        return (
            mainItem &&
            ((mainItem.uniqueId && !secondArray.some((secondItem) => mainItem.uniqueId === secondItem.UniqueId)) ||
                (mainItem.userId && !secondArray.some((secondItem) => mainItem.userId === secondItem.Id)))
        );
    });

    return updatedMainArray;
}

You can play with it here.

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function updateMainObject(mainArray: any[], secondArray: any[]) {
    const updatedMainArray = mainArray.filter((mainItem) =&gt; {
    return (
      mainItem &amp;&amp;
      ((mainItem.uniqueId &amp;&amp; !secondArray.some((secondItem) =&gt; mainItem.uniqueId === secondItem.UniqueId)) ||
      (mainItem.userId &amp;&amp; !secondArray.some((secondItem) =&gt; mainItem.userId === secondItem.Id)))
    );
  });

  return updatedMainArray;
}

<!-- end snippet -->

You can play with it there

huangapple
  • 本文由 发表于 2023年8月4日 22:55:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837054.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定