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

189
Vistas
Given two equal objects, filter only the differences

Lets say I have two different objects

let obj1 = {
  "value1" : "1",
  "value2" : "2",
  "value3" : "3",
  "value4" : "4"
}

let obj2 = {
  "value1" : "1",
  "value2" : "3",
  "value3" : "3",
  "value4" : "5"
}

I need a the differences in a new object that will depend on obj2

result = { "value2":"3" , "value4" : "5" }

The case can be too , that obj1 === undefined In that case, I need all the values from obj2

Currently I have

let result = {}
let singleChange = false

for (const i of Object.entries(obj2)) {
    if (obj1 && obj1[i[0]] !== i[1]) {
      result [i[0]] = i[1];
      singleChange = true
    } else if(!singleChange) {
      result = obj2;
    }
  }

But it doesnt really sem to work properly all the times. Is there a better way to write this?

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

0

A modern version with entries and fromEntries:

let obj1 = {
  "value1" : "1",
  "value2" : "2",
  "value3" : "3",
  "value4" : "4"
}

let obj2 = {
  "value1" : "1",
  "value2" : "3",
  "value3" : "3",
  "value4" : "5"
}

let obj3 = undefined;

function objIntersection(o1, o2) {
  const filteredArray = Object.entries(o2)
  .filter(([k,v]) => o1?.[k] !== v);

  return Object.fromEntries(filteredArray);
}

console.log(objIntersection(obj1, obj2));
console.log(objIntersection(obj3, obj2));

about 4 years ago · Juan Pablo Isaza Denunciar

0

const obj1 = {
  value1: '1',
  value2: '2',
  value3: '3',
  value4: '4',
};

const obj2 = {
  value1: '1',
  value2: '3',
  value3: '3',
  value4: '5',
};
// find only the values that are different
const diff = (obj1, obj2) => {
  const obj3 = {};
  for (const key in obj1) {
    if (obj1[key] !== obj2[key]) {
      obj3[key] = obj2[key];
    }
  }
  return obj3;
}
console.log(diff(obj1, obj2));

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could get the entries of the second object and filter with the first one.

const
    getDifference = (a, b) => Object.fromEntries(Object
        .entries(b)
        .filter(([k, v]) => a[k] !== v)
    ),
    obj1 = { value1 : "1", value2 : "2", value3 : "3", value4 : "4" },
    obj2 = { value1 : "1", value2 : "3", value3 : "3", value4 : "5" },
    result = getDifference(obj1, obj2);

console.log(result);

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