Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda