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

339
Vistas
Lodash isEqual - How to get keys of objects that differ

I'm trying to obtain an array of keys, that were different as a result of comparing two objects. I was only able to do it using isEqualWith and mapKeys, like so:

const differencies = [];
const objA = {
  a: 1,
  b: 2,
  c: 3
};
const objB = {
  a: 1,
  b: 2,
  c: 100
};

function customizer(objOne, objTwo) {
  if (lodash.isEqual(objOne, objTwo)) {
    return true;
  }

  lodash.mapKeys(objOne, (value, key) => {
    if (value !== objTwo[key]) {
      differencies.push(key);
    }
  });

  return false;
}

lodash.isEqualWith(objA, objB, customizer);
console.log(differencies);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
<script>const lodash = _;</script>

Question is: Is there a better way to get keys of objects that differ using lodash?

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

0

Since you only need to compare objects where the keys are identical but values might differ, you can leverage keys() or keysIn() (if you want to also traverse the prototype chain) and remove all entries that do not have a matching value with filter().

const objA = { a: 1, b: 2, c: 4 };
const objB = { a: 1, b: 2, c: 100 };

const differencies = lodash.filter(
  lodash.keys(objA), 
  key => objA[key] !== objB[key]
);
console.log(differencies);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
<script>const lodash = _;</script>

Alternatively using chaining syntax:

const objA = { a: 1, b: 2, c: 4 };
const objB = { a: 1, b: 2, c: 100 };

const differencies = lodash(objA)
  .keys()
  .filter(key => objA[key] !== objB[key]);
console.log(differencies);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
<script>const lodash = _;</script>

For this sort of functionality, Lodash might be an overkill. Simple JavaScript equivalent is Object.keys() and Array#filter():

const objA = { a: 1, b: 2, c: 4 };
const objB = { a: 1, b: 2, c: 100 };

const differencies = Object.keys(objA)
  .filter(key => objA[key] !== objB[key]);
console.log(differencies);

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