-1

I have a list of arrays i need to hide the duplicates of array

   {
        "company_name": "SERVICE INDUSTRIES LTD.",
        "claim_id": "2017\/04\/LHRHHDP00015-2018-00702",         
    },
    {

        "company_name": "KARACHI CHAMBER OF COMMERCE & INDUSTRY",
        "claim_id": "2018\/03\/HOHHDP00013-2019-00098",

    },
    {
        "company_name": "PAKISTAN RED CRESCENT SOCIETY",
        "claim_id": "2017\/04\/LHRHHDP00015-2018-00702",

    },
    {
        "company_name": "SERVICE INDUSTRIES LTD.",
        "claim_id": "2018\/04\/LHRHHDP00022-2019-01292",     
    },
    {
        "company_name": "U MICROFINANCE BANK LTD",
        "claim_id": "2017\/04\/LHRHHDP00015-2018-00702",    
    }

This is the example array i need to hide the array which have duplicate claim_id.

Jota.Toledo
  • 27,293
  • 11
  • 59
  • 73
Umaiz Khan
  • 1,319
  • 3
  • 20
  • 66

2 Answers2

1

You can use a filter and findIndex to find duplicates. If the index does not equal the current item, there is a duplicate.

The performance impact is maximum 1.5x the size of the array:

const unique = data.filter((item, index) => 
  data.findIndex(({ claim_id }) => item.claim_id === claim_id) === index
);
Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149
0

To solve this easily let's use this strategy:

  • Extract values from the array to an object (used as a dictionary)
  • Eliminating duplication
  • Extract values to a new array

so....

const dic = {}

for (const item of originalArray) {
    dic[item.claim_id] = item
}

const groupedArray = Object.values(dic)

The only question is... where's typescript? we used native JS and managed just fine :)

ymz
  • 6,602
  • 1
  • 20
  • 39
  • Thanks for your answer but I think this code doubled the arrays. Mean i have 13701 arrays after using this code its showing above 20,000 arrays – Umaiz Khan Aug 07 '19 at 07:24
  • you should update your question and add the code you applied so it will be as clear as possible what exactly what's going on – ymz Aug 07 '19 at 07:26