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

168
Vistas
Difference between two Objects - reduce

I've 2 Objects with data that is repeated but also varies. How to compare them and get the differences?

const obj1 = {
  surname: "kowalski",
  name: "adam",
  age: 23,
  city: "Wroclaw",
  country: "Poland",
};

const obj2 = {
  name: "adam",
  age: 34,
  city: "Warszawa",
  country: "Poland",
  friend: "Ala",
};

const objCombined = { ...obj1, ...obj2 };

I've to use .reduce.

My work:

const find = Object.entries(objCombined).reduce((diff, [key]) => {
  if (!obj2[key]) return diff;

  if (obj1[key] !== obj2[key]) diff[key] = obj2[key];

  return diff;
}, {});

but the output is without surname: "kowalski". Expected output:

{surname: "kowalski", age: 34, city: "Warszawa", friend: "Ala"}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Please use this code

const find = Object.entries(objCombined).reduce((diff, [key]) => {
  if (!obj2[key]) {
    diff[key] = obj1[key];
    return diff;
  }

  if (obj1[key] !== obj2[key]) diff[key] = obj2[key];

  return diff;
}, {});
about 4 years ago · Juan Pablo Isaza Denunciar

0

Remove if (!obj2[key]) return diff and add obj1[key]; if obj2[key]; is undefined:

const obj1 = {
  surname: "kowalski",
  name: "adam",
  age: 23,
  city: "Wroclaw",
  country: "Poland",
};

const obj2 = {
  name: "adam",
  age: 34,
  city: "Warszawa",
  country: "Poland",
  friend: "Ala",
};

const objCombined = { ...obj1, ...obj2 };

const find = Object.entries(objCombined).reduce((diff, [key]) => {
//  if (!obj2[key]) return diff;

  if (obj1[key] !== obj2[key]) diff[key] = obj2[key] || obj1[key];

  return diff;
}, {});

console.log(find);

about 4 years ago · Juan Pablo Isaza Denunciar

0

We can use Object.keys() and Array.reduce() to get the difference between the two objects:

const obj1 = { surname: "kowalski", name: "adam", age: 23, city: "Wroclaw", country: "Poland", };
const obj2 = { name: "adam", age: 34, city: "Warszawa", country: "Poland", friend: "Ala", };

const diff = [...Object.keys(obj1), ...Object.keys(obj2)].reduce((acc, key) => { 
    if (obj1[key] !== obj2[key]) {
       acc[key] = obj2[key] || obj1[key];
    }
    return acc;
}, {})

console.log("Diff:", diff);

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