英文:
In Angular compare two different objects and remove duplicates
问题
在Angular中,我有这两个包含对象的数组。
- 主要对象
[
{
"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": ""
},
{},
{},
{},
{},
{},
{},
{},
]
- 第二个对象
[
{
"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.
-
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": ""
},
{},
{},
{},
{},
{},
{},
{},
] -
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) => {
return (
mainItem &&
((mainItem.uniqueId && !secondArray.some((secondItem) => mainItem.uniqueId === secondItem.UniqueId)) ||
(mainItem.userId && !secondArray.some((secondItem) => mainItem.userId === secondItem.Id)))
);
});
return updatedMainArray;
}
<!-- end snippet -->
You can play with it there
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论