Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

345
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!