1

I have an object array with duplicates. The data at the root level of the objects are identical, but the nested array of objects are not. That is what I need to merge before running a lodash uniqBy or whatever function to remove the duplicates.

This is the object array with duplicates.

    [
    {
        "id": "66E175A2-A29F-4F1A-AD81-2422B1EB00F6",
        "name": "College Park / Brookhaven",
        "mktId": 0,
        "status": "Unknown",
        "code": "197D6",
        "ownershipType": null,
        "series": [
            {
                "id": "80004F2E-E3C8-4B6A-BCCC-81259AEAF22D",
                "name": "01",
                "productType": "Detached",
                "productClass": "Single Family",
                "salesStartDate": null,
                "modelOpenDate": null
            }
        ]
    },
    {
        "id": "E053E656-4D14-4F2A-AD70-A37F65195CD1",
        "name": "College Park / Hampshire",
        "mktId": 0,
        "status": "Unknown",
        "code": "316D6",
        "ownershipType": null,
        "series": [
            {
                "id": "46830FBD-CD68-4D4C-A095-FB9C3D93D01A",
                "name": "02,03",
                "productType": "Detached",
                "productClass": "Single Family",
                "salesStartDate": null,
                "modelOpenDate": null
            }
        ]
    },
    {
        "id": "E053E656-4D14-4F2A-AD70-A37F65195CD1",
        "name": "College Park / Hampshire",
        "mktId": 0,
        "status": "Unknown",
        "code": "316D6",
        "ownershipType": null,
        "series": [
            {
                "id": "1BC31692-AAB8-4A00-9D8D-9B8CF7E426E0",
                "name": "01",
                "productType": "Detached",
                "productClass": "Single Family",
                "salesStartDate": null,
                "modelOpenDate": null
            }
        ]
    },
    {
        "id": "34F7C7AF-3D1B-4EE7-8271-C99294169C01",
        "name": "College Park / Hillsdale",
        "mktId": 0,
        "status": "Unknown",
        "code": "295D6",
        "ownershipType": null,
        "series": [
            {
                "id": "807144A1-26ED-4657-9775-7DF7563107D3",
                "name": "02",
                "productType": "Detached",
                "productClass": "Single Family",
                "salesStartDate": null,
                "modelOpenDate": null
            }
        ]
    }
]

College Park / Hampshire is duplicated in this example. I need to find the 2 duplicates and return this expected result..

   [
    {
        "id": "66E175A2-A29F-4F1A-AD81-2422B1EB00F6",
        "name": "College Park / Brookhaven",
        "mktId": 0,
        "status": "Unknown",
        "code": "197D6",
        "ownershipType": null,
        "series": [
            {
                "id": "80004F2E-E3C8-4B6A-BCCC-81259AEAF22D",
                "name": "01",
                "productType": "Detached",
                "productClass": "Single Family",
                "salesStartDate": null,
                "modelOpenDate": null
            }
        ]
    },
    {
        "id": "E053E656-4D14-4F2A-AD70-A37F65195CD1",
        "name": "College Park / Hampshire",
        "mktId": 0,
        "status": "Unknown",
        "code": "316D6",
        "ownershipType": null,
        "series": [
            {
                "id": "46830FBD-CD68-4D4C-A095-FB9C3D93D01A",
                "name": "02,03",
                "productType": "Detached",
                "productClass": "Single Family",
                "salesStartDate": null,
                "modelOpenDate": null
            },
            {
                "id": "1BC31692-AAB8-4A00-9D8D-9B8CF7E426E0",
                "name": "01",
                "productType": "Detached",
                "productClass": "Single Family",
                "salesStartDate": null,
                "modelOpenDate": null
            }
        ]
    },
    {
        "id": "34F7C7AF-3D1B-4EE7-8271-C99294169C01",
        "name": "College Park / Hillsdale",
        "mktId": 0,
        "status": "Unknown",
        "code": "295D6",
        "ownershipType": null,
        "series": [
            {
                "id": "807144A1-26ED-4657-9775-7DF7563107D3",
                "name": "02",
                "productType": "Detached",
                "productClass": "Single Family",
                "salesStartDate": null,
                "modelOpenDate": null
            }
        ]
    }
]

vanilla javascript or lodash is fine.

1

2 Answers 2

1

I've created HashMap using reduce, in that hashMap, the key is the name of the item, the value is the item itself.

const data = [{
    "id": "66E175A2-A29F-4F1A-AD81-2422B1EB00F6",
    "name": "College Park / Brookhaven",
    "mktId": 0,
    "status": "Unknown",
    "code": "197D6",
    "ownershipType": null,
    "series": [{
      "id": "80004F2E-E3C8-4B6A-BCCC-81259AEAF22D",
      "name": "01",
      "productType": "Detached",
      "productClass": "Single Family",
      "salesStartDate": null,
      "modelOpenDate": null
    }]
  },
  {
    "id": "E053E656-4D14-4F2A-AD70-A37F65195CD1",
    "name": "College Park / Hampshire",
    "mktId": 0,
    "status": "Unknown",
    "code": "316D6",
    "ownershipType": null,
    "series": [{
      "id": "46830FBD-CD68-4D4C-A095-FB9C3D93D01A",
      "name": "02,03",
      "productType": "Detached",
      "productClass": "Single Family",
      "salesStartDate": null,
      "modelOpenDate": null
    }]
  },
  {
    "id": "E053E656-4D14-4F2A-AD70-A37F65195CD1",
    "name": "College Park / Hampshire",
    "mktId": 0,
    "status": "Unknown",
    "code": "316D6",
    "ownershipType": null,
    "series": [{
      "id": "1BC31692-AAB8-4A00-9D8D-9B8CF7E426E0",
      "name": "01",
      "productType": "Detached",
      "productClass": "Single Family",
      "salesStartDate": null,
      "modelOpenDate": null
    }]
  },
  {
    "id": "34F7C7AF-3D1B-4EE7-8271-C99294169C01",
    "name": "College Park / Hillsdale",
    "mktId": 0,
    "status": "Unknown",
    "code": "295D6",
    "ownershipType": null,
    "series": [{
      "id": "807144A1-26ED-4657-9775-7DF7563107D3",
      "name": "02",
      "productType": "Detached",
      "productClass": "Single Family",
      "salesStartDate": null,
      "modelOpenDate": null
    }]
  }
];

const hashMap = data.reduce((hash, item) => {
  const key = item.name;
  if (hash.hasOwnProperty(key)) {
    hash[key].series = hash[key].series.concat(item.series);
  } else {
    hash[key] = item;
  }
  return hash;
}, {});

const result = Object.keys(hashMap).map((key) => hashMap[key]);

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

0

Just set up a hashmap for the ids and a resulting array:

const hash = {}, result = [];

Then iterate over the array:

 for(const el of array){
   const { id, series } = el;

Now we can check if the id already appears in the hash, and if so just add the series:

   if(hash[id]){
     hash[id].series.push(...series);
   } else {

If the id did not appear yet, we need to add the object to our result and to the hashtable:

     result.push(hash[id] = el)
  }
}

And thats already it :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.