Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove duplicates in nested objects of object

Trying to parse some network descriptions from devices, hitting a problem where a description could be duplicate across types. This causes errors further down the road in code. What is the best choice with lodash or javascript to compare each nested object and remove duplicates until they don’t exist in the arrays. The goal is that string 1 – 111 would be removed until it only exists in one object.

const data = {
    "hostname": {
        "typeA": [
            "string 1 - 111",
            "string 2 - 222",
            "string 3 - 333"
        ],
        "typeB": [
            "string 1 - 111",
            "string 4 - 444",
            "string 5 - 555"
        ],
        "typeC": [
            "string 1 - 111",
            "string 6 - 666"
        ]
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You simply need to iterate over each type array and filter based on a Set of previously seen values. Here mutating data in place using filter() to both populate the Set and to return the filtered array.

const data = {
  hostname: {
    typeA: ['string 1 - 111', 'string 2 - 222', 'string 3 - 333'],
    typeB: ['string 1 - 111', 'string 4 - 444', 'string 5 - 555'],
    typeC: ['string 1 - 111', 'string 6 - 666'],
  },
};

const seen = new Set();

for (const key of Object.keys(data.hostname)) {
  data.hostname[key] = data.hostname[key].filter((s) => {
    const _seen = seen.has(s);
    seen.add(s);

    return !_seen;
  });
}

console.log(data);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading