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

172
Vistas
Javascript remove object keys that are not in both objects with Lodash?

So, I have this two objects:

const obj1 = {
  foo: 'Foo',
  bar: {
    baz: 'Baz',
  }
}

const obj2 = {
  foo: 'Foo',
  bar: {
    baz: 'Baz',
    zip: 'Zip'
  },
}

I want to remove "zip" from obj2 since it's not in obj1. Is there a way to do this with lodash?

EDIT: If a property is an object, it will be an object in both of them. There can't be a string with the same key of an object in the other one.

The use case is, I have a translation file and after some processing, I need to remove the ones that are no longer needed. So if I delete them in the original object (i.e. obj1) they have to be removed from the other ones (in this case, obj2.

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

0

If you are open to using additional libraries, I can recommend looking into deep-diff:

const obj1 = {
  foo: 'Foo',
  bar: {
    baz: 'Baz',
  }
}

const obj2 = {
  foo: 'Foo',
  bar: {
    baz: 'Baz',
    zip: 'Zip'
  },
}

DeepDiff.observableDiff(obj2, obj1, d => {
  DeepDiff.applyChange(obj2, obj1, d)
});

console.log(obj1)
console.log(obj2)
<script src="https://cdn.jsdelivr.net/npm/deep-diff@1/dist/deep-diff.min.js"></script>

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