javascriptangularobjectfilterobject-comparison

In Angular compare two different objects and remove duplicates


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.


Solution

  • 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 there