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

192
Visualizações
Efficient way to merge dictionaries while removing duplicates using javascript?

Given an array of dictionaries in javascript, I want to merge them so that they don't contain any duplicates. For example given

jsons1 = [{"a": "xyz"}, {"a": 12}]
jsons2 = [{"a": "xyz"}, {"a", 13}]

I want to return a new array that removes the duplicates while merging

jsons3 = [{"a": "xyz"}, {"a": 12}, {"a": 13}]

order isnt important

This is what I did:

let jsons1 = [
    {
        "a": "xyz"
    
    }, 
    {
        "a": 4
    
    }, 
    {
        "a": 1
    
    }, 
  
]


let jsons2 = [
    {
        "a": "xyz"
    
    }, 
    {
        "a": 4
    
    }, 
    {
        "a": 6
    
    }, 
  
]

const val = [jsons1, jsons2]

let filtered = []
for(json of val) {
    for(obj of json) {
    let match = false;
    for(dict of filtered) {
        if(JSON.stringify(dict) === JSON.stringify(obj)) {
        match = true;
        break
      }
    }
    if(match == true) {
        continue
    }
    filtered.push(obj)

    }
}

console.log(filtered)

https://jsfiddle.net/awb6qnzo/6/

Basically I create a new array called filtered and I then iterate through each jsons array. I then iterate through filtered to check if that obj is already in it.

although it works, its extremely inefficient. Is there a simpler way?

Edit: Changing question to specific values since the former is apparently not possible

Given an array of dictionaries in javascript, I want to merge them so that they don't contain any duplicates (on the key "id"). E.g.

jsons1 = [{"id": 1}, {"id": 12}]
jsons2 = [{"id": 1}, {"id": 3}]

I want to return a new array that removes the duplicates while merging

jsons3 = [{"id": 1}, {"id": 3}, {"id": 12}]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Let's reword the problem and pretend we have a function equals that checks if two objects are the same, first.

We want to concatenate all the values in jsons2 that satisfy some condition.

To do that, we need to filter out objects that don't satisfy the condition.

This condition is that every object in jsons1 does not equal the object in jsons2.

Another way to put this is the negation of some object in jsons1 is equal to the object in jsons2.

jsons1.concat(
    jsons2.filter(
        (a) => !jsons1.some((b) => equals(a, b))
    )
);

You can write equals anyway you want, as long as it functions correctly.

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