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

185
Vistas
Comparing Two Lists, Updating if Different

I have a program written in JavaScript that runs CRUD operations depending if the items in one lists are different from the other. Basically comparing two lists, main list and mirror list. The constraints are:

  • If an object is in the main list, but not the mirror list, add it to the mirror list

  • If an object is in the mirror list, but not in the main list, delete it from the mirror list

What would be an efficient way to perform this check? Currently, I am iterating through two for loops which seems inefficient.

for (const mainId of Object.keys(mainItems)) {
      if (!Object.keys(mirrorItems).includes(mainId)) {
        await createEvent(mainItems[mainId])
      }
}

for (const mirrorId of Object.keys(mirrorItems)) {
      if (!Object.keys(mainItems).includes(mirrorId)) {
        await deleteEvent(mirrorItems[mirrorId])
      }
}

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

From your codes it looks like you are comparing keys of objects, not just arbitrary arrays. Therefore you would want mirrorItems to have the same keys as mainItems but not the same values (otherwise that would just be a copy).

Using a Set which performs lookups in O(1) promotes your solution from a solution running in O(n²) to one running in O(n).

Additionally you need reduce().

const obj = {
  matching: "A matching property but a diferrent value",
  missing: "A property which is missing in mirror, should be added",
};

const mirror = {
  matching:
    "A matching property but a diferrent value, to see that there is not just a copy created",
  additional: "An additional property which should be deleted",
};

const set = new Set(Object.keys(mirror));
const correctMirror = Object.keys(obj).reduce(
  (mirrored, cur) => (
    set.has(cur)
      ? // if the property exist on the obj, we can keep the current value
        (mirrored[cur] = mirror[cur])
      : // if the property does not exist on obj, we need to get the value from obj and assign it to the mirror
        (mirrored[cur] = obj[cur]),
        // return mirrored for next iteration or result
    mirrored
  ),
  {}
);
console.log(correctMirror);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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