Search code examples
javascripttypescriptobjectkeylodash

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/[email protected]/lodash.min.js"></script>
<script>const lodash = _;</script>

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


Solution

  • 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/[email protected]/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/[email protected]/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);