Home > Net >  Remove duplicates by comparing two arrays
Remove duplicates by comparing two arrays

Time:10-12

I have 2 arrays (rlT and refundT). I want to remove the duplicate items in the rlT array. All the items in the rlT array with the transactionId matching the transactionId of the items in the refundT array must be removed. I have a working code with filter() and find() by following the link: Remove all elements contained in another array. I want to implement the same functionality using Promise.all and find().

let refundT = [
    {
        "_id": "616149cfd0c4e7178757254f",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "reloadOrderId": "9a493445",
        "processType": "Inbound",
        "transactionId": "a1ae05ce",
        "paymentMethod": "Credit/Debit card",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "2",
        "channelName": "MM",
        "createdAt": "2021-10-09T07:50:39.104Z",
        "updatedAt": "2021-10-09T08:30:02.948Z",
        "__v": 0,
        "errorCode": "-",
        "errorDescription": "Cron job EXPIRED the transaction"
    },
    {
        "_id": "616138e1f5cdaf66bd455deb",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "reloadOrderId": "712e27c9d17d6ab3",
        "processType": "Inbound",
        "transactionId": "23nea9r27cBz",
        "paymentMethod": "DCB",
        "transactionAmount": 25,
        "conversionWalletValue": 2500,
        "processStatus": "1",
        "channelName": "DCB",
        "respField01": "516337616341058476",
        "walletExpiry": "2021-10-14T06:38:25.581Z",
        "createdAt": "2021-10-09T06:38:25.501Z",
        "updatedAt": "2021-10-09T06:38:25.581Z",
        "__v": 0,
        "currentBalance": 26,
        "currentBalanceCoins": 2600
    },
    {
        "_id": "61601ba49d149212e6a465d3",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "reloadOrderId": "a507f4a549489924",
        "processType": "Inbound",
        "transactionId": "1qqJk0XdZ1zO",
        "paymentMethod": "DCB",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "1",
        "channelName": "DCB",
        "respField01": "516336886107955938",
        "walletExpiry": "2021-10-13T10:21:24.601Z",
        "createdAt": "2021-10-08T10:21:24.571Z",
        "updatedAt": "2021-10-08T10:21:24.602Z",
        "__v": 0,
        "currentBalance": 5,
        "currentBalanceCoins": 500
    },
    {
        "_id": "6160086eb371321df17e8e28",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "reloadOrderId": "97187f4b",
        "processType": "Inbound",
        "transactionId": "e8292dd8",
        "paymentMethod": "Credit/Debit card",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "2",
        "channelName": "M",
        "createdAt": "2021-10-08T08:59:26.859Z",
        "updatedAt": "2021-10-08T10:30:00.499Z",
        "__v": 0,
        "errorCode": "-",
        "errorDescription": "Cron job EXPIRED the transaction"
    },
    {
        "_id": "616002116ddd8d16dfbd3bdb",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "reloadOrderId": "5264ee47",
        "processType": "Inbound",
        "transactionId": "2d141956",
        "paymentMethod": "Credit/Debit card",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "2",
        "channelName": "MM",
        "createdAt": "2021-10-08T08:32:17.065Z",
        "updatedAt": "2021-10-08T10:30:00.522Z",
        "__v": 0,
        "errorCode": "-",
        "errorDescription": "Cron job EXPIRED the transaction"
    },
    {
        "_id": "616001f96ddd8d16dfbd3bda",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "reloadOrderId": "5dd468f8",
        "processType": "Inbound",
        "transactionId": "8634c9a4",
        "paymentMethod": "Credit/Debit card",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "2",
        "channelName": "MM",
        "createdAt": "2021-10-08T08:31:53.548Z",
        "updatedAt": "2021-10-08T12:54:41.623Z",
        "__v": 0,
        "errorCode": "-",
        "errorDescription": "Cron job EXPIRED the transaction"
    },
    {
        "_id": "61600051942ddd0ce73d8730",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "reloadOrderId": "784dc7bf",
        "processType": "Inbound",
        "transactionId": "7140c4a8",
        "paymentMethod": "Credit/Debit card",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "2",
        "channelName": "MM",
        "createdAt": "2021-10-08T08:24:49.009Z",
        "updatedAt": "2021-10-08T12:54:41.665Z",
        "__v": 0,
        "errorCode": "-",
        "errorDescription": "Cron job EXPIRED the transaction"
    },
    {
        "_id": "615fff84942ddd0ce73d872f",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "reloadOrderId": "1e069bc1",
        "processType": "Inbound",
        "transactionId": "557798c5",
        "paymentMethod": "Credit/Debit card",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "2",
        "channelName": "MM",
        "createdAt": "2021-10-08T08:21:24.524Z",
        "updatedAt": "2021-10-08T12:54:41.684Z",
        "__v": 0,
        "errorCode": "-",
        "errorDescription": "Cron job EXPIRED the transaction"
    },
    {
        "_id": "615ffee8942ddd0ce73d872e",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "reloadOrderId": "eeb8102d",
        "processType": "Inbound",
        "transactionId": "61049097",
        "paymentMethod": "Credit/Debit card",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "2",
        "channelName": "MM",
        "createdAt": "2021-10-08T08:18:48.800Z",
        "updatedAt": "2021-10-08T12:54:41.693Z",
        "__v": 0,
        "errorCode": "-",
        "errorDescription": "Cron job EXPIRED the transaction"
    },
    {
        "_id": "615ffe76942ddd0ce73d872b",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "reloadOrderId": "7d7c18ac",
        "processType": "Inbound",
        "transactionId": "38e63c72",
        "paymentMethod": "Online banking",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "2",
        "channelName": "MM",
        "createdAt": "2021-10-08T08:16:54.399Z",
        "updatedAt": "2021-10-08T12:54:41.696Z",
        "__v": 0,
        "errorCode": "-",
        "errorDescription": "Cron job EXPIRED the transaction"
    },
    {
        "_id": "615ffd43942ddd0ce73d872a",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "reloadOrderId": "9c2d02a0",
        "processType": "Inbound",
        "transactionId": "a4099cb1",
        "paymentMethod": "Online banking",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "2",
        "channelName": "MM",
        "createdAt": "2021-10-08T08:11:47.281Z",
        "updatedAt": "2021-10-08T13:30:00.048Z",
        "__v": 0,
        "errorCode": "-",
        "errorDescription": "Cron job EXPIRED the transaction"
    }
]
let rlT = [
    {
        "_id": "6161368fe775582a8a56b660",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "6ee3b80be60804d9",
        "processType": "Inbound",
        "transactionId": "2iTmr4gPyqL2",
        "paymentMethod": "DCB",
        "transactionAmount": 10,
        "conversionWalletValue": 1000,
        "processStatus": "3",
        "respField01": "516337610298568370",
        "createdAt": "2021-10-09T06:28:31.575Z",
        "updatedAt": "2021-10-09T06:28:31.575Z",
        "__v": 0
    },
    {
        "_id": "61613615e775582a8a56b65d",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "d419dac30da92876",
        "processType": "Inbound",
        "transactionId": "01XuNwWK7T11",
        "paymentMethod": "DCB",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "3",
        "respField01": "516337609079578348",
        "createdAt": "2021-10-09T06:26:29.535Z",
        "updatedAt": "2021-10-09T06:26:29.535Z",
        "__v": 0
    },
    {
        "_id": "616135bae775582a8a56b65a",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "a9739187c9f16c3b",
        "processType": "Inbound",
        "transactionId": "4DAT1sCgI78A",
        "paymentMethod": "DCB",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "3",
        "respField01": "516337608140338332",
        "createdAt": "2021-10-09T06:24:58.398Z",
        "updatedAt": "2021-10-09T06:24:58.398Z",
        "__v": 0
    },
    {
        "_id": "616008afb371321df17e8e2a",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "31ff569c4f94aefb",
        "processType": "Inbound",
        "transactionId": "e8292dd8",
        "paymentMethod": "Credit/Debit card",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "3",
        "txnNo": "D92940",
        "createdAt": "2021-10-08T09:00:31.308Z",
        "updatedAt": "2021-10-08T09:00:31.308Z",
        "__v": 0
    },
    {
        "_id": "616005dc256d9f1c2f463c4c",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "206dc8760b30336e",
        "processType": "Inbound",
        "transactionId": "2d141956",
        "paymentMethod": "Credit/Debit card",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "3",
        "txnNo": "D92932",
        "createdAt": "2021-10-08T08:48:28.263Z",
        "updatedAt": "2021-10-08T08:48:28.263Z",
        "__v": 0
    },
    {
        "_id": "6160056e7b4ede1be0b17994",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "c05f52ba6016ebe1",
        "processType": "Inbound",
        "transactionId": "2d141956",
        "paymentMethod": "Credit/Debit card",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "3",
        "txnNo": "D92932",
        "createdAt": "2021-10-08T08:46:38.066Z",
        "updatedAt": "2021-10-08T08:46:38.066Z",
        "__v": 0
    },
    {
        "_id": "6160054dc8b3001baae803f5",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "189723181bc7236f",
        "processType": "Inbound",
        "transactionId": "2d141956",
        "paymentMethod": "Credit/Debit card",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "3",
        "txnNo": "D92932",
        "createdAt": "2021-10-08T08:46:05.977Z",
        "updatedAt": "2021-10-08T08:46:05.977Z",
        "__v": 0
    },
    {
        "_id": "61600478a82ef91b223713d0",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "c517dbbe47182851",
        "processType": "Inbound",
        "transactionId": "2d141956",
        "paymentMethod": "Credit/Debit card",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "3",
        "txnNo": "D92932",
        "createdAt": "2021-10-08T08:42:32.700Z",
        "updatedAt": "2021-10-08T08:42:32.700Z",
        "__v": 0
    },
    {
        "_id": "6160032de1605518303a778d",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "491951af5e824c64",
        "processType": "Inbound",
        "transactionId": "2d141956",
        "paymentMethod": "Credit/Debit card",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "3",
        "txnNo": "D92932",
        "createdAt": "2021-10-08T08:37:01.441Z",
        "updatedAt": "2021-10-08T08:37:01.441Z",
        "__v": 0
    },
    {
        "_id": "616002f38a9da217fdfaf29e",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "4c249cf7085b9359",
        "processType": "Inbound",
        "transactionId": "2d141956",
        "paymentMethod": "Credit/Debit card",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "3",
        "txnNo": "D92932",
        "createdAt": "2021-10-08T08:36:03.107Z",
        "updatedAt": "2021-10-08T08:36:03.107Z",
        "__v": 0
    },
    {
        "_id": "616002716ddd8d16dfbd3bdd",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "ffc9b8a6beeba0f5",
        "processType": "Inbound",
        "transactionId": "2d141956",
        "paymentMethod": "Credit/Debit card",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "4",
        "txnNo": "D92932",
        "errorCode": "0",
        "errorDescription": "Success",
        "createdAt": "2021-10-08T08:33:53.462Z",
        "updatedAt": "2021-10-08T08:33:53.462Z",
        "__v": 0
    },
    {
        "_id": "61600066942ddd0ce73d8732",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "a3ad562dae470c7f",
        "processType": "Inbound",
        "transactionId": "557798c5",
        "paymentMethod": "Credit/Debit card",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "4",
        "txnNo": "D92930",
        "errorCode": "0",
        "errorDescription": "Success",
        "createdAt": "2021-10-08T08:25:10.168Z",
        "updatedAt": "2021-10-08T08:25:10.168Z",
        "__v": 0
    },
    {
        "_id": "615ffed6942ddd0ce73d872d",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "24f1ab09cf9a0dff",
        "processType": "Inbound",
        "transactionId": "38e63c72",
        "paymentMethod": "Online banking",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "4",
        "txnNo": "MH92922",
        "errorCode": "1",
        "errorDescription": "Failed",
        "createdAt": "2021-10-08T08:18:30.778Z",
        "updatedAt": "2021-10-08T08:18:30.778Z",
        "__v": 0
    },
    {
        "_id": "615ffbfa942ddd0ce73d8728",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "44659dab5701ad5e",
        "processType": "Inbound",
        "transactionId": "b66292aJ2d3a",
        "paymentMethod": "DCB",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "3",
        "respField01": "516336804968754530",
        "createdAt": "2021-10-08T08:06:18.520Z",
        "updatedAt": "2021-10-08T08:06:18.520Z",
        "__v": 0
    },
    {
        "_id": "615ffbf1942ddd0ce73d8725",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "414c92d467cabd92",
        "processType": "Inbound",
        "transactionId": "12Xo212z6n01",
        "paymentMethod": "DCB",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "3",
        "respField01": "516336804856904527",
        "createdAt": "2021-10-08T08:06:09.003Z",
        "updatedAt": "2021-10-08T08:06:09.003Z",
        "__v": 0
    },
    {
        "_id": "615ffb4e942ddd0ce73d8722",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "6afd27ae07297062",
        "processType": "Inbound",
        "transactionId": "0F14mCe1bmLV",
        "paymentMethod": "DCB",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "3",
        "respField01": "516336803248314498",
        "createdAt": "2021-10-08T08:03:26.138Z",
        "updatedAt": "2021-10-08T08:03:26.138Z",
        "__v": 0
    },
    {
        "_id": "615ffb3e942ddd0ce73d871f",
        "epmId": "615ff4a8836c1b2d46a353f8",
        "referenceId": "168c0f1b70919ecb",
        "processType": "Inbound",
        "transactionId": "9003kMf1628N",
        "paymentMethod": "DCB",
        "transactionAmount": 5,
        "conversionWalletValue": 500,
        "processStatus": "3",
        "respField01": "516336803064324494",
        "createdAt": "2021-10-08T08:03:10.895Z",
        "updatedAt": "2021-10-08T08:03:10.895Z",
        "__v": 0
    }
]
let myArray1 = rlT.filter(ar => !refundT.find(rm => (rm.transactionId === ar.transactionId) ))
console.log(myArray1.length)

I tried the Promise.all() as below:

let result = await Promise.all( 

    rlT.map(async(element) => {
        let dup = refundT.find((e) => {
            console.log('dup', e.transactionId, element.transactionId)
            return e.transactionId == element.transactionId
        })
        if(dup){
            console.log('kkkk')
        }
       // console.log('dup', dup)
       
    })
    )

I am able to find the duplicates, but not successful in removing the duplicate items from the rlT array.

CodePudding user response:

If both arrays have been fetched, this can be done synchronously. filter returns all of the elements of an array meeting a test. In the OP case, the test is whether the element id is not found in the refund array.

let result = rlT.filter(t => {
  return !refundT.find(r => r.transactionId === t.transactionId)
});

CodePudding user response:

You may also consider using a hash table if the list are very large.

const m = new Map();
rlT.forEach(t => m.set(t.transactionId, t));
refundT.forEach(t => m.delete(t.transactionId));

console.log(Array.from(m.keys()))
console.log(Array.from(m.values()))
  • Related