Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

278
Visualizações
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"
        ]
    }
}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda